29 Standard JSP Tags

Pravin Jain

epgp books

 

Introduction

 

The taglib directive is used only when we are using some kind of library. There is a provision for using tag. A programmer could create tags then the web page designer, the JSP designer uses those tags. If you want to use tags developed by another developer, he will be using <% @taglib .. %> to include the tags. In this module we are going to look at standard tags. These do not require taglibs, these are tag which are available to a JSP developer. They need not to include anything additional to use these tags. just develop the JSP and use these tags which are always available.

 

In the MVC, what we have seen in the role of the Servlet is, Servlet receives the request, does processing and after doing processing gets the result in the form of java beans. Java Beans are nothing but the data component. Then it get forwarded to the JSP. It would set them as an attribute. The result would be set as attribute because these are the things which have to be rendered. These are the data objects are then to be rendered by the JSP to show in the JSP.

 

So, the servlet would be creating these objects rather fetching these objects by doing some processing. Once we have fetched those objects, set them as attributes in request or session most of the time in a request or in a session and then forward it to the next JSP. If instead of forwarding if you are using redirect then it would always be session which has to be used because it is a different request in case of a redirect.

 

We have java beans component being set up and then shared through a request or a session object to be used by a JSP page. Now the JSP page has to show the various values the various properties from the java beans component which it is receiving from a servlet. through a request or a session object. So how do we render java bean components.

 

The bean tags

 

For that the most commonly used tags we categorize under tags which are available as part of JSP. We categorize then as bean tags.

 

We have got three bean tags: jsp:useBean

jsp:getProperty jsp:setProperty

These are the three tags related to the java bean.

 

The resource tags

 

We have two more tags which are categorize as resource tags:

 

jsp:forward

jsp:include

 

The plugin tags

 

We have another tag which is related to the component like you might be including applet or something for that we have tag:

 

jsp:plugin

 

If we have any java bean component we can use jsp:useBean tag which is to be rendered in a JSP. At the beginning of such a jsp page what we have normally where some kind of java bean components are available. We can use tag called jsp:useBean.

 

The attributes of jsp:useBean tag

 

This tag, jsp:useBean has several attributes, class – which would specify the class of the java bean, which java bean component class it is. It would have an attribute called id = ‘java bean name’, It would have another attribute to specify scope – in which scope is this attribute coming from. Is this java bean coming from some tags, java beans are also going to be created by the JSP. what the jsp does, you are specifying the class, scope, also name so it will see by this name do we have an attribute. In this particular scope and if it is of this type, if it is there it is going to use something similar to getAttribute method for the scope what are values available for the scope, we use to specify the attribute for the scope. There are only three things using which we can share the objects say using request, session or ServletContext which is nothing but a application object. We have four values available for the scope say :

  1. scope = ‘page’ – it is smallest scope.
  2. scope = ‘request’
  3. scope = ‘session’
  4. scope = ‘application’

 

page scope – if there is something shared within the page say if you want to share something with another tag say standard tags, so when we use these tags these tags have their own life cycle and these life-cycle has the scope within the page itself. So if there is something to be shared within the scope of the page for that reason we got in jsp pages the scope called page also.

 

When we were developing only using Servlet we had only three scopes that was request, session and application. Now we have page also.

 

In the JSP whenever we use jsp:useBean we are specifying an attribute called class, name of the bean. If there is an attribute by that name and if we have object by this name and it is of the same class as specified in the class and whatever scope we specify it must find that attribute there. If it does not find such an attribute in that case this being a simple java bean component it is able to create an object of that class using the default constructor and put it in that scope.

 

So, jsp:useBean is going to do a check if it is there or not. If it is there then it becomes available from this point onwards.

 

jsp:useBean this component is now usable in this jsp page. If the component was not existing not found in that scope it would create new one. If it was found in the scope it would pick it up and not create the new. So, if some component would be shared it would simply be able to use it and if the component shared by using attributes.

 

So the Servlet might have set the attributes which we are now getting it with the help of jsp:useBean. So the jsp:useBean attributes were class, name, scope.

 

Another thing which we can also specify is type=, but whenever you specify type it should be some super type or same as the class. If you don’t specify it is considered same as the class.

 

When developing a JSP page in the JSP page, in the initial part if we want to use certain component, if we are going to render value from a particular component which is available as an attribute we will use the jsp:useBean. From this point onwards it is going to be available.

 

The attributes of jsp:getProperty tag

 

Another tag which is bean tag is jsp:getProperty, we use it like <jsp:getProperty name = ‘name-of-the- bean’ and then we say property = and here we specify which property value is to be rendered. So here jsp:getProperty with two attributes. name – the name of the bean specified as id in the useBean tag earlier property – the property of the java bean object to be rendered This requires that jsp:useBean must be specified first. jsp:getProperty is always after jsp:useBean tag. In jsp:useBean we don’t have name= but it is id=

 

The attributes of jsp:setProperty tag

 

Third is jsp:setProperty, it is used for changing the value of the property. Changing the value of the property in a particular bean component.

 

If we are following an MVC pattern, JSPs role is only of rendering and displaying. jsp:setProperty is not about showing something so it is not something that conforms to the MVC pattern. In jsp:setProperty we have an attribute called name= specifying the name of the bean. So if there is jsp:useBean we can use jsp:setProperty.

 

We have property= which property has to be changed, which is the property whose value is to be set and then value= through which we can set the value of the property. What we specify in value= some hard coded value.

 

Are we going to have some fixed value to be set?. If we are having the use of the jsp:setProperty that means this component is more likely to be something which has been created here. It is not something shared by servlet and here setting it out. So we would like to set it up something as a part of a request. May be it is some parameter in the request.

 

So, whatever is the value of the parameter in the request, it is something what we would like to set as a value here. We don’t want to say value= and give some fixed value but rather we would like to have whatever is the value of the parameter going to be set to this particular property.

 

So instead of value= we have param= specifying the name of the parameter so it will pick up the value corresponding to the parameter from the request and set it for the value of the property specified on this bean object. So, we got these three bean tags.

 

jsp:useBean jsp:setProperty jsp:getProperty

 

Let us move to the resource tag now. We have two resource tag: jsp:forward

 

jsp:include

 

This is like the JSP does not want to do the processing but do want to forward to some other resource. So, we got something called jsp:forward or may be JSP want to include some part from some other jsp or resources. This is similar to RequestDispatcher. We would like to use something similar to RequestDispatcher. In RequestDispatcher we had two methods:

 

public void forward(ServletRequest request, ServletResponse response) public void include(ServletRequest request, ServletResponse response)

 

The attributes of jsp:forward tag

 

So same thing can be done within JSP simply by using tags. We have a tag called jsp:forward. we have the attribute in jsp:forward called page=. So we can say page=… . So, let us say from this JSP we want to forward to some other JSP saying I don’t want to render and let it be done by some other JSP.

 

The jsp:param tag

 

At time when we say jsp:forward we might like to pass some request parameter may be those that are not actually there in the request, but when someone says request.getParameter() it should be available to the next resource. If we want to pass some parameters to the resource we have some inner tag within the JSP, jsp:forward tag. Within the JSP even in jsp:include we can use this tag as an nested tag, this tag is jsp:param. We can say jsp:param with the attributes called name= to the name of the parameter and value= can create a value. So, we are able to create new parameters and specify value for the parameter which will be available to the next resource. So we can have jsp:forward which specify a page within that we can have inner tag called jsp:param.

 

The attributes of jsp:include tag

 

Similarly, in jsp:include, this also has something called page. this also has page= and you specify may be a next resource. This next resource may be another JSP page. If it is jsp:include it is not necessarily the JSP it might be any other resource but when it is a include it means i would like to include a partial content provided by some other resource.

 

So you have some other resources and I am forwarding this whole jsp within that some part is to be rendered by picking up some part of the content asking some other resource to generate the content. Whatever is generated that is being included here. As far as jsp:include is concerned it also have one more attribute called flush=, so flush = ‘true’ means before it starts including the content it will flush out whatever is there so far before jsp:include is there.

 

The attributes of jsp:plugin tag

 

We might also like to include applet to our page we can use jsp:plugin tag. It has the attribute called type=. So type= it would be applet then like the attribute for the applet tag which we use in normal html page. Here we also have an attribute called code= we can specify width, height, align, halign, valign. The tags which we have for normal applet all are available over here. Here we also have a provision to specify jre version like jreversion= the version required for our applet component.

 

So we can have jsp:plugin type=’applet’ code= width= etc… all such attributes will be specified and here we also specify jreversion. jreversion is the minimum version needed by this. As far as applet tag is concerned applet has the inner tag called param so here also we would like to have parameter available for our applet through the JSP tags.

 

So through this JSP tag if we have said jsp:plugin type=’applet’ we can include parameters here.

 

So we have tag called jsp:params and then it has an inner tag called jsp:param. The jsp:param will have name= value=. So to include parameters in our applet we have jsp:params with inner tags jsp:param.

you can view video on Standard JSP Tags

Suggested Reading:

  1. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  2. Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education
  3. Java Server Programming for Professionals by Ivan Bayross, Sh aranam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.
  4. http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html