Action Element in JSP

JSP actions can be used to print a script expression, create and store a Java Bean and for many other things. They are used for constructs in XML syntax to control the behavior of the Servlet engine.

Using JSP actions
  • a file can be inserted into another page dynamically,
  • a bean component can be reused,
  • a user can be forwarded from one page to another page.
Following are the JSP action tags:

Action tagsDescription
<jsp:include>Includes a file at the time when the page is requested.
<jsp:useBean>Finds the object of Java bean from given scope or create a new object.
<jsp:setProperty>Sets the property of a JavaBean object.
<jsp:getProperty>Prints the property of the Java bean object.
<jsp:forward> Forwards the request and response to another resource.
<jsp:plugin>It is used when there is a need of a plugin to run a Bean class.
<jsp:param>Sets the parameter value to the request. It is used in forward and include mostly.
<jsp:fallback> Supplies alternate text if Java applet is unavailable on the client.
<jsp:element>Defines XML element directly.

The <jsp:include> Action Tag

Syntax:
<jsp:include page="page URL"  flush="Boolean Value" />

Example : Illustrating the <jsp:include> tag

//welcome.jsp

<p>
    Today's date: <%= new java.util.Date()%>
</p>

//demo.jsp

<html>
     <head>
         <title>JSP include Action Tag</title>
     </head>
<body>
     <h3>JSP page: Demo Include</h3>
     <jsp:include page = "welcome.jsp" flush="false" />
</body>
</html>

The <jsp:forward> Action Tag

Syntax:
<jsp:forward page="Relative URL" />

Example : Illustrating the <jsp:forward> action tag

<html>
     <head>
        <title>JSP Forward Action Tag</title>
     </head>
<body>
     <h2>JSP page: Demo forward</h2>
     <jsp:forward page="welcome.jsp" />
</body>
</html>

The <jsp:setProperty> Action Tag

It is used to set the property of a Bean.

Syntax:
<jsp: useBean id="instanceOfBean" class="package_name.class_name" />
....
....
<jsp:setProperty name="instanceOfBean" property="property_name" />

The <jsp:getProperty> Action Tag

It is used to return the value of the property.

Syntax:
<jsp:getProperty name="instanceOfBean" property="propertyName" />