Monday, May 13, 2013

Jsp Action, Custom tags and EL expressions

To remove the java code from jsp, Sun micro system has given jsp action tags.

Jsp Action tags:

The following are the jsp action tags to use as part of a jsp.

--> <jsp:include/>
--> <jsp:forward/>
--> <jsp:useBean/>
--> <jsp:setProperty/>
--> <jsp:getProperty/>


<jsp:include/>

is used to include the contents of two jsp's and send the result to the client (browser).

syntax:


'page' attribute specifies the name of the jsp to which the content from the calling jsp has to be included.

When ever jsp compiler has encountered the , it will evaluate the tag (convert the tag into corresponding servlet code) and includes the content of two jsp's.


<jsp:forward/>

is used to forward a request from one resource to another resource (like RequestDispathcer). Instead of java code (RequestDispatcher) in jsp , we can use and   to include and forward the request.

syntax:


'page' attribute specifies the resource (jsp or html or servlet) to which the request has to be forwarded.

When evre jsp compiler has encountered the it will evaluate the tag and forwards the request to corresponding resource given to the path attribute.


<jsp:useBean/>

Create a java bean 'SampleBean' with two properties name and rollno.


Now, if we want to create object to this java bean as part of our jsp, as we know we should not write java code in jsp (like SampleBean bean = new SampleBean() ), we will take the help of

syntax:


class attribute specifies name of the class to which object has to be created, id attribute specifies unique id to the created object to use it further and the scope attribute specifies type of scope (page/request/session/application).

When ever jsp compiler encountered the , it will evaluates the tag and get's the class from class attribute and container will create object with the given id within the given scope.

<jsp:setProperty/>

is used to set the value of a property specified in corresponding java bean.

syntax:


property attribute specifies the name of the property in the java bean (name here), name attribute specifies name (id) of the bean object (sampBean) and value attribute specifies the value given to the property('addicted' here).

Whenever jsp compiler encountered the it will evaluates the tag to corresponding servlet code and checks the name of property in the java bean and set's the value supplied to the value attribute.


<jsp:getProperty/>

is used to get the property value from the corresponding java bean specified.

syntax:


property attribute specifies the name of the property in the java bean, name attribute specifies the name(id) of the bean object.

Jsp compiler will evaluates the tag ,get's the value of the property from the java bean and displays in the browser.


Jsp Action tag's are sufficient ..?

To avoid the java code from the jsp , jsp action tags are not sufficient. If we use action tags we can't remove some java code like jsp expressions(<%= =>) .That's why concept of custom tags came into the picture.


Jsp Custom Tags:

Custom tags are the user defined tags, which can be used to remove the java code as part of jsp.

We can also create our own tags by following some rules.

Create your own custom tag:

If we want to create our own custom tags, we need to have the following.

--> a tld( taglib directive) file and
--> a jar file consisting of the tag handler classes

creating tld file:

To create the tld file, we need to follow the jsp custom tag specification. The following is the custom tld file we have created.


<taglib> is the root tag which has child tags like <tlib-version>,<uri>,<tag> etc.

Each <tagelement has child tags like <name><tag-clas>,<attribute> etc.

creating Tag Handler class: 

A tag handler class is a class which provides the implementation of javax.servlet.jsp.tagext.Tag interface directly or indirectly.

If our class is implementing javax.servlet.jsp.tagext.Tag interface, everytime we need to override some couple of methods repeatedly as shown.


Instead of implementing all the methods, Sun microsystem itself has given a predefiend class javax.servlet.jsp.tagext.TagSupport implementing Tag interface.

So if we create a class extending javax.servlet.jsp.tagext.TagSupport it will become tag handler class.


PerformTaskTagHandler is the custom tag handler class which has a property text, since we have only one attribute text ,for the tag perfom in the tld file.

Number of properties in the tag handler class depends on the number of attributes to that class in tld file.

To use the custom tag library in our application we can take the help of taglib directive.


uri attribute specifies the resource location( tld location), whatever uri we specify in tld file, that has to be mentioned in uri attribute.

prefix attribute is used to use the tag as part of our jsp as shown above.

Once the tag got evaluated by jsp compiler, the corresponding tag handler class PerformTaskTagHandler  will be called and doEndTag() will be executed.

In doEndTag() we are returning int value as EVAL_PAGE. We can also return it as SKIP_PAGE.

EVAL_PAGE refers to once doEndTag() got executed ,jsp compiler will evaluate the remaining tags in the jsp.

SKIP_PAGE refers to , after execution of doEndTag() jsp compiler will skip all the tags without evaluating.

Whatever the task we want to perform with the help of our custom tag, we will write code to that task as part of doEndTag().


EL Expressions:

EL (Expression Language) expressions are used to deal with scopped variables.


Scopped variables:

A variable with a specific scope is called scopped variable. we have four different scopped variables available.

They are :

--> Page scope
--> Request scope
--> Session scope and
--> Application scope

The scope (availability) of page scope is within that page only. The scope of request scope is within that request that is executing. 

The scope of session scope is with in that browser for any request.
The scope of application scope is within that application from any browser and any request.

Suppose we want to deal with scopped variables as part of jsp, instead of writing java code like out.println(request.getAttribute("keyname")), we can take the help of EL expressions.

syntax:


simply use ${} to use EL expressions. Here, sampBean is the name of the object which was stored in reuest scope.

Whenever jsp compiler encounters EL expressions it will check for the key(sampBean) in scopes folloeing this order page,request,session,application.

If it founds key (sampBean) in page scope it will stop searching in remaining scopes.

Or if we know in which scope the key was available we can directly give


Now jsp compiler will only check in request scope.

pageScope ,
requestScope,
sessionScope and
applicationScope are the implicit objects avaialable in jsp to deal with scopped variables.

In order to use the EL expressions in jsp we need to make sure that isElIgonred attribute to false.

4 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information. best java training in chennai | best java training in velachery

    ReplyDelete
  3. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    java training in chennai | java training institutes in chennai | java j2ee training institutes in velachery

    ReplyDelete