JSP

JSP Projects

JSP Project

What is Action tag in JSP?
Previous Home Next

 

Action tag transfer the control between the pages .It transfer the control from one page to the other particular  page.

JSP Action Tags:
There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks.
The action tags are used to control the flow between pages and to use Java Bean. The Jsp action tags are given below.

JSP Action Tags Description
jsp:forwardforwards the request and response to another resource.
jsp:useBeancreates or locates bean object.
jsp:includeincludes another resource.
jsp:setPropertysets the value of property in bean object.
jsp:getPropertyprints the value of property of the bean.
jsp:pluginembeds another components such as applet.
jsp:paramsets the parameter value. It is used in forward and include mostly.
jsp:fallbackcan be used to print the message if plugin is working. It is used in jsp:plugin.


note:The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we will see these tags in bean developement.Here we select only important JSP action tags.

jsp:forward action tag:

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource.

Syntax of jsp:forward action tag without parameter

<jsp:forward page="relativeURL | <%= expression %>" />

Syntax of jsp:forward action tag with parameter

<jsp:forward page="relativeURL | <%= expression %>">

<jsp:param name="parametername" value="parametervalue | <%=expression%>" />

</jsp:forward>


Example: jsp:forward action tag without parameter

In this example, we are simply forwarding the request to the printdate.jsp file.

index.jsp
 

<html>

<body>

<h2>this is index page</h2>

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

</body>

</html>

printdate.jsp

<html>

<body>

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>

</body>

</html>

Example: jsp:forward action tag with parameter

In this example, we are forwarding the request to the printdate.jsp file with parameter and printdate.jsp file prints the parameter value with date and time.

index.jsp
 

<html>

<body>

<h2>this is index page</h2>

<jsp:forward page="printdate.jsp" >

<jsp:param name="name" value="ycd.com" />

</jsp:forward>

</body>

</html>

printdate.jsp

<html>

<body>

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>

<%= request.getParameter("name") %>

</body>

</html>

 There are various Action tags but most usable tags are only three:
Include Action tag :-Include Action tag is used to include either static  or dynamic contents.
Syntax
<jsp:include page="{relativeURL | <%= expression %>}" flush="true" />
If the included file is a static file than the contents are included as such n the JSP file and if file is dynamic than the request is pass to the dynamic file as the included action completed result sent back.
for dynamic page the Syntax of include action tag is 
<jsp:include page="{relativeURL | <%= expression %>}" flush="true" />
<jsp:include name="parameter name" value="parameter |<%expression" />

Forward Action Tag :-This tag forward the control to the static or dynamic resource.
Syntax
<jsp:forward page="{relativeURL | <%= expression %>}" />
 &nbsp;

Usebean Action tag :-This tag allows JSP to create an instance  or to receive an instance of java bean.It is also used to create and initiate the bean with specific name and  scope.

Syntax of Usebean Action Tag 
<jsp:useBean
     id="beanInstanceName"
     scope="page|request|session|application"
     {
        class="package.class" |
        type="package.class" |
        class="package.class" type="package.class" |
        beanName="{package.class | <%= expression %>}" 
        type="package.class"
     }
     { /> |
     > other elements
     </jsp:useBean>

 

Previous Home Next