JSP Action Tags – jsp useBean, include, forward

Filed Under: Java EE

JSP provides a bunch of standard action tags that we can use for specific tasks such as working with java bean objects, including other resources, forward the request to another resource etc.

JSP action tags

jsp action tags, jsp standard action
The list of standard JSP action elements are given below.

JSP Action Description
jsp:include To include a resource at runtime, can be HTML, JSP or any other file
jsp:useBean To get the java bean object from given scope or to create a new object of java bean.
jsp:getProperty To get the property of a java bean, used with jsp:useBean action.
jsp:setProperty To set the property of a java bean object, used with jsp:useBean action.
jsp:forward To forward the request to another resource.
jsp:text To write template text in JSP page.
jsp:element To define the XML elements dynamically.
jsp:attribute To define the dynamically generated XML element attributes
jsp:body To define the dynamically generated XML element body
jsp:plugin To generate the browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.

Mostly in JSP programming, we use jsp:useBean, jsp:forward and jsp:include action. So we will focus on these action elements only.

JSP useBean

We can use jsp:useBean like below in JSP pages to get the bean object.


<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="request" />

In the above example, JSP container will first try to find the myBeanAttribute in the request scope but if it’s not existing then it will create the instance of MyBean and then assign it to the myBeanAttribute id variable in JSP and sets it as an attribute to the request scope.

Once the bean is defined in JSP, we can get its properties using jsp:getProperty action like below.


<jsp:getProperty name="myBeanAttribute" property="count" />

Notice that name attribute in jsp:getProperty should be same as id attribute in the jsp:useBean action. JSP getProperty action is limited because we can’t get the property of a property, for example if MyBean has a property that is another java bean, then we can’t use JSP action tags to get it’s value, for that we have JSP EL.

We can use jsp:setProperty to set the property values of a java bean like below.

<jsp:setProperty name="myBeanAttribute" property="count" value="5" />

If we want to set the property only if jsp:useBean is creating a new instance, then we can use jsp:setProperty inside the jsp:useBean to achieve this, something like below code snippet.


<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="request">
<jsp:setProperty name="myBeanAttribute" property="count" value="5" />
</jsp:useBean>

Most of the times we code in terms of interfaces, so if we have Person interface and Employee implementation and we are setting a request attribute like below.


Person person = new Employee();
request.setAttribute("person", person);

Then we can use type attribute with jsp:useBean to get the java bean object like below.


<jsp:useBean id="person" type="Person" class="Employee" scope="request" />

If we don’t provide scope attribute value in jsp:useBean, it’s defaulted to page scope.

If we want to set the Java Bean properties from request parameters, we can use param attribute like below.


<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="id" param="empID" />
</jsp:useBean>

If property and param attribute values are same, we can skip the param attribute. For example if request parameter name is also id then we can simply write:


<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="id" />
</jsp:useBean>

If all the request parameter names match with the java bean properties, then we can simply set bean properties like below.


<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="*" />
</jsp:useBean>

JSP include

We can use jsp:include action to include another resource in the JSP page, earlier we saw how we can do it using JSP include Directive.

Syntax of jsp:include action is:


<jsp:include page="header.jsp" />

The difference between JSP include directive and include action is that in include directive the content to other resource is added to the generated servlet code at the time of translation whereas with include action it happens at runtime.

We can pass parameters to the included resource using jsp:param action like below.


<jsp:include page="header.jsp">
   <jsp:param name="myParam" value="myParam value" />
</jsp:include>

We can get the param value in the included file using JSP Expression Language.

JSP forward

We can use jsp:forward action tag to forward the request to another resource to handle it. Its syntax is like below.


<jsp:forward page="login.jsp" />

That’s all for a quick roundup of JSP action tags. We will look into JSP Standard Tag Library (JSTL) in the future post.

Comments

  1. shekhar says:

    Hi Pankaj
    Great work. Can you provide downloadable link to you tutorials in the form of pdf. If it is possible that would be great help to readers .

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages