Happy Republic Day 2015

MBA I SEM UNIV EXAMS HELD ON 30-JAN,2015

MBA III SEM UNIV EXAMS HELD ON 29-JAN,2015

Tuesday, September 25, 2012

J2ee: Introduction to JSP tags

 The basic JSP component is the JSP tags. I (JBS) will provide you the general overview about the same. Here is a listing of the tags used in Java Server Pages:
  1. Declaration tag
  2. Expression tag
  3. Directive tag
  4. Scriptlet tag
  5. Action tag

Declaration tag:
Declaration tag is used to define functions, methods and variables that will be used in Java Server Pages.
Notation of the Declaration tag is given below:

"<%! ... %>"

General syntax of Declaration Tag:

<%!
// Variables or methods declaration
// This section will be launched at the traslated servlet class directly
Java statement 1;
Java statement 2;
...;
Java statement n;
%>

REMEMBER: Every Java statement will be terminated with (;)

For example:

<%!
private int n = 0 ;
public int meth( int sth)
{
//Java Statements ;
}
%>


Expression tag:
Expression tag is used to display output of any data on the generated page. The data placed in Expression tag prints on the output stream and automatically converts data into string. The Expression tag can contain any Java expression used for printing output equivalent to out.println().Thus, an expression tag contains a scripting language expression which is evaluated, automatically converts data to a String and the outputs are displayed.
Notation of Expression tag is given below:

<%= ... %>

General syntax of Expression Tag:

<%=
// This section will bw launched within the out.print() method, within _jspservice() method, within the translated servlet class
//Java Expression to be printed out
%>


For example:

<%=new java.util.Date() %>


Directive tag:
Directives are JSP elements that provide global information about an entire JSP page. An example would be a directive that indicated the language to be used in compiling a JSP page. The syntax of a directive is as follows:

<%@ directive {attribute="value"} %>

This states that, for this page directive, assign these values for these attributes. A directive can contain n number of optional attribute/value pairs. The following line of code would indicate that the JSP language to use would be Java:

<%@ page language="java" %>

There are three possible directives currently defined by the JSP specification: page, include, and taglib. Each one of these directives and their attributes, if applicable, are defined in the following sections.

  • The page Directive-
The page directive defines information that will be globally available for that JavaServer Page.

  • The include Directive-
The include directive is used to insert text and code at JSP translation time. The syntax of the include directive is as follows:

<%@ include file="relativeURLspec" %>

The file that the file attribute points to can reference a normal text HTML file or it can reference a JSP file, which will be evaluated at translation time.

  • The taglib Directive-
The most recent version of the JSP specification defines a mechanism for extending the current set of JSP tags. It does this by creating a custom set of tags called a tag library. That is what the taglib points to. The taglib directive declares that the page uses custom tags, uniquely names the tag library defining them, and associates a tag prefix that will distinguish usage of those tags. The syntax of the taglib directive is as follows:

<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %>


Scriptlet tag:
Scriptlets are what bring all the scripting elements together. They can contain any coding statements that are valid for the language referenced in the language directive. They are executed at request-time and they can make use of declarations, expressions, and JavaBeans. The syntax for a scriptlet is as follows:

<% scriptlet source %>

During the initial request the JSP scripting code is converted to servlet code and then compiled and loaded into resident memory. The actual source code, which is found between scriptlet tags <% ... %>, is placed into the newly created servlet's _jspService() method. See the following sample JSP source:

<% out.println("HELLO WORLD"); %>

It has a very simple scriptlet section that will print HELLO WORLD to the JspWriter implicit objectout.
You don't need to dig too deeply into the servlet code generated by the Web server, because it is generated for you. You just need to understand that it is being generated by the JSP engine and is the JSP equivalent to a servlet's service() method. It is also important to know that the JSP engine creates a servlet equivalent to the init() and destroy() methods.

Action tag:
Actions provide an abstraction that can be used to easily encapsulate common tasks. They typically create or act on objects, normally JavaBeans. The JSP technology provides some standard actions. These actions are defined in the following sections.

  • *jsp:useBean*
The *jsp:useBean* action associates an instance of a JavaBean defined with a given scope and ID, via a newly declared scripting variable of the same ID.

  • *jsp:setProperty*
The *jsp:setProperty* action sets the value of a bean's property.

  • *jsp:getProperty*
The *jsp:getProperty* action takes the value of the referenced bean instance's property, converts it to a java.lang.String, and places it into the implicit out object.

  • *jsp:include*
The *jsp:include* action provides a mechanism for including additional static and dynamic resources in the current JSP page. The syntax for this action is as follows:

*jsp:include page="urlSpec" flush="true" /*
and
*jsp:include page="urlSpec" flush="true"*
{ jsp:param ... /> }
*/jsp:include*

The first syntax example illustrates a request-time inclusion, whereas the second contains a list of param sub-elements that are used to argue the request for the purpose of inclusion.
N.B. Consider the stars as opening and closing angular brackets.