JSP: Action Elements

We have discussed about JSP scripting elements and directive elements in our previous articles. In this article, I am going to discuss about JSP action elements.

Action elements are another type of syntactic elements; translated into Java code that performs an operation. The web-server, process these action elements when processing a client’s HTTP request.

Here we are going to discuss about nine JSP pre-defined standard action elements and their usage. We can also develop our own actions called custom actions in JSP by using pre-defined classes available in JSP.

JSP Action tags are used for runtime communication between:

  • JSP page to JSP page
  • JSP page to a Servlet
  • JSP page to a Java Applet
  • JSP page to a Java Bean

JSP Actions are three types:

  • Standard actions
  • Custom actions and
  • JSP Standard Tag Library (JSTL)

In JSP, there are nine standard actions are available; all actions are prefixed with “jsp:”. We no need to import any tag library while using JSP standard actions. The nine JSP standard action elements are:

  1. <jsp:useBean>
  2. <jsp:setProperty>
  3. <jsp:getProperty>
  4. <jsp:param>
  5. <jsp:forward>
  6. <jsp:include>
  7. <jsp:plugin>
  8. <jsp:fallback>
  9. <jsp:params>

1. <jsp:useBean>

This action element declares a new JSP scripting variable and associates a Java object to it.

For example:

<jsp:useBean id=”mb” class=”MyBean” scope=”request”/>

Above statement creates an instance of a class “MyBean”; it is same as the below Java statement:

MyBean mb = new MyBean();

If “MyBean” class with object name as “mb” is already available in “request” scope, that object will be returned; otherwise new object of “MyBean” class will create and store it into the “request” scope. What do you mean by “request” scope? Like normal variables declared in a program; Bean objects declared through action tags also have the scope. In case of normal variables; the scope is block scope, function scope etc.,; but the Bean objects defined through action tags have page, request, session and application scopes.

While creating web applications, with JSP pages, usually we put the business logic and presentation logic both in the JSP pages only. Mixing both business logic and presentation logic in JSP pages will seriously raise re-usability issues. That means, if multiple JSP pages need same business logic then we have to repeat the same business logic in multiple JSP pages. This is a serious re-usability issue.

To solve this problem we can use JSP with a Java Bean communication. In this, we place presentation logic into JSP page and the business logic into Java Bean. So, business logic becomes reusable and can be used in multiple JSP pages.

If we want to invoke methods of a Java Bean from a JSP page, we need an object of the Bean in the JSP Page; <jsp:useBean> tag is used for creating a Java Bean object. <jsp:useBean> tag will first search for an object in the given scope; if it exists, then it uses the same object; otherwise, it creates a new object and then stores it in the given scope.

Bean scopes in JSP page are: page, request, session and application.

2. <jsp:setProperty>

<jsp:setProperty> tag is used for calling setter methods of a Java Bean. This tag must be used under <jsp:useBean> tag. This tag can set on both static and dynamic values of a Java Bean.

Attributes of this tag are:

  • name
  • property
  • param and
  • value

“name” and “property” attributes are mandatory. “param” and “value” attributes are not allowed at a time. One more important thing to remember is; Bean’s “id” and value of “name” attribute should be same.

For example:

<jsp:useBean id=”mb” class=”com.codesteps.MyBean” scope=”request">

    <jsp:setProperty name=”mb” property=”sno” value=”56”/>
    <jsp:setProperty name=”mb” property=”age” param=”age”/>


In the first tag, inside the <jsp:useBean> action element, we are assigning a static value “56″ to “sno” property of “MyBean” by calling Java Bean’s setter method, using <jsp:setProperty> action element.

Another statement, sets the value of a request parameter “age” to “age” property of “MyBean”. We can also write this statement as:

<jsp:setProperty name="mb" property="age" />

because the request parameter name “age” matched with the property name “age” of a Java Bean.

If suppose, all request parameters are matched with the property names of a Java Bean, we can write the statement as:

<jsp:setProperty name="mb" property="*" />

If any uncommon property name is present, we need to separately assign the value to that particular uncommon property.

3. <jsp:getProperty>

Like <jsp:setProperty>, <jsp:getProperty> also works on Java Bean properties or attributes. This tag is used to retrieve the values of Java Bean attributes. This tag calls getter methods of a Java Bean. It should be used at outside of <jsp:useBean> tag; unlike <jsp:setProperty> tag, which should be used inside of <jsp:useBean> tag.

<jsp:getProperty> tag has only two attributes; “name” and “property” attributes. “name” attribute assign with Java Bean’s “id” and “property” attribute assign with the property name of a Java Bean.

For example:

<jsp:getProperty name=”mb” property=”sno”/>

This statement calls the getter method of “sno” attribute or property through “mb” object (MyBean object) and returns the “sno” value to the web-browser; then the web-browser will display the returned value.

One important thing to remember, when using <jsp:getProperty> tag is; unlike <jsp:setProperty>, we are not allowed to write “property=”*”” in <jsp:getProperty> tag.

4. <jsp:param>

<jsp:param> tag is used to provide additional information to the JSP page by appending information to the request object (HttpServletRequest object). Generally we use <jsp:param> tag within <jsp:include>, <jsp:forward>, or <jsp:plugin> tags.

For example:

<jsp:param name="key" value="6x7y8z" />

This statement sets the “key” parameter with the value “6x7y8z”.

5. <jsp:forward>

This action tag is used to forward an HTTP request from one JSP page to another JSP page, or a Servlet, or an HTML page. Internally this action tag will use RequestDispatcher’s “forward()” method to forward the requests.

If a JSP page need more logic, then we divide the logic into multiple pages and then we forward the requests between the JSP pages.

Along with the request, the request parameters sent from web-browser will be forwarded. If we want to forward a request with additional parameters then we can use <jsp:param> action tag under <jsp:forward> tag; then the parameters mentioned in <jsp:param> tag will forward with the request to another resource (A JSP page, or a Servlet, or an HTML page.).

For Example:

<jsp:forward page=”one.jsp”/>

This will forward the request to a JSP page “one.jsp” without adding any additional parameters.

And another example here is:

<jsp:forward page=”one.jsp”>
    <jsp:param name=”sno” value=”90”/>

This will forward the request to “one.jsp” by adding additional parameter “sno” with the value “90”.

6. <jsp:include>

<jsp:include> action tag is used to include the content of another resource (that means the response of another resource) into the JSP page.

For example:

<jsp:include page=”one.jsp” />

This will include the response of “one.jsp” into the current JSP page.

We can also send parameters to another resource. For example:

<jsp:include page=”one.jsp”>
    <jsp:param name=”sname” value=”steve”/>

This will include the response of “one.jsp” JSP page; and observe that we are passing a parameter “sname” to “one.jsp” using <jsp:param> action element.

Remember that include action element is different than include directive. As discussed, include action element is used to include the response of the resource; where as include directive is used to include the resource. That’s why, include action element is more useful for including dynamic content (even we can include static content); where as include directive is useful for including only static content.

For example: Below is the include directive which includes “anotherfile.jsp”.

<%@include file=”anotherfile.jsp”/>

We have discussed more about include directive in “JSP: Directive Elements” article.

7. <jsp:plugin>

<jsp:plugin> tag is used to embed a Java component (an Applet or a Java Bean), inside a JSP page. We can pass arguments or parameters to the embedded object using <jsp:param> tag. <jsp:param> tag must be inside the <jsp:params> tag which should be inside the <jsp:plugin> tag.

This action element generates an HTML code that contains either <object> or <embed> tag;  as appropriate for the requesting client web-browser.

By any reason, if an embedded component is not shown in the web-browser, then we can set an alternate message to display in the web-browser with the help of <jsp:fallback> tag. <jsp:fallback> tag is only allowed inside the <jsp:plugin> tag.

For example:

Let’s assume we have a “SampleApplet” Java applet. When we place the below code in our JSP page; and access the page, it will show a “SampleApplet” in the web-browser, if it is avaialble. Otherwise, it will simply display a message “Unable to load the applet.” in the web-browser.

<jsp:plugin type="applet" code="SampleApplet" width="500" height="300">
        <jsp:param name="width" value="200"/>
        <jsp:param name="height" value="70"/>
    <jsp:fallback>Unable to load the applet.</jsp:fallback>

We have discussed more about Java Applets in “Java: Applet – Introduction and it’s life-cycle” article.

8. <jsp:fallback>

As mentioned above, <jsp:fallback> tag is used to display an alternate message when the Java component is not working in the JSP page.

For example:

<jsp:fallback>Unable to load the component.</jsp:fallback>

This tag must be placed inside the <jsp:plugin> tag.

9. <jsp:params>

This tag is used to add additional parameters to the request object; under this tag we have to use <jsp:param> tag to provide additional parameters information.

These are the standard action elements available in JSP.

We will discuss about JSTL and custom actions in our upcoming articles.

Leave a Reply