JSP Actions lets you perform some action.
Directives vs Actions
- Directives are used during translation phase while actions are used during request processing phase.
- Unlike Directives Actions are re-evaluated each time the page is accessed.
The following are the action elements used in JSP:
1. <jsp:include> Action
Like include page directive this action is also used to insert a JSP file in another file.
<jsp:include> vs include directive :
It has the same difference which I mentioned at the beginning of the article (directive vs action). In <jsp:include> the file is being included during request processing while in case of include directive it has been included at translation phase.
Syntax of <jsp:include> :
<jsp:include page="page URL" flush="Boolean Value" />
Here page URL is: the location of the page needs to be included & flush value can be either true or false (Boolean value).
Example:
<html> <head> <title>Demo of JSP include Action Tag</title> </head> <body> <h3>JSP page: Demo Include</h3> <jsp:include page="sample.jsp" flush="false" /> </body> </html>
page: Page value is sample.jsp which means this is the page needs to be included in the current file. Just the file name mentioned which shows that the sample.jsp is in the same directory.
flush: Its value is false, which means resource buffer has not been flushed out before including to the current page.
Read more: jsp include action tag.
2. <jsp:forward> Action
<jsp:forward> is used for redirecting the request. When this action is encountered on a JSP page the control gets transferred to the page mentioned in this action.
Syntax of <jsp:forward> :
<jsp:forward page="URL of the another static, JSP OR Servlet page" />
Example:
first.jsp
<html> <head> <title>Demo of JSP Forward Action Tag</title> </head> <body> <h3>JSP page: Demo forward</h3> <jsp:forward page="second.jsp" /> </body> </html>
Now when JSP engine would execute first.jsp (the above code) then after action tag, the request would be transferred to another JSP page (second.jsp).
Note: first.jsp and second.jsp should be in the same directory otherwise you have to specify the complete path of second.jsp.
Read more: JSP forward action tag.
3. <jsp:param> Action
This action is useful for passing the parameters to Other JSP action tags such as JSP include & JSP forward tag. This way new JSP pages can have access to those parameters using request object itself.
Syntax of <jsp:param>:
<jsp: param name="param_name_here" value="value_of_parameter_here" />
Now considers the same above example –
first.jsp
<html> <head> <title>Demo of JSP Param Action Tag</title> </head> <body> <h3>JSP page: Demo Param along with forward</h3> <jsp:forward page="second.jsp"> <jsp:param name ="date" value="20-05-2012" /> <jsp:param name ="time" value="10:15AM" /> <jsp:param name ="data" value="ABC" /> </jsp:forward> </body> </html>
In the above example first.jsp is passing three parameters (data, time & data) to second.jsp and second.jsp can access these parameters using the below code –
Date:<%= request.getParameter("date") %> Time:<%= request.getParameter("time") %> My Data:<%= request.getParameter("data") %>
4. <jsp:useBean> Action
Read more here – <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty> in detail.
This action is useful when you want to use Beans in a JSP page, through this tag you can easily invoke a bean.
Syntax of <jsp:useBean>:
<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name" />
Example of <jsp:useBean>, <jsp:setProperty> & <jsp:getProperty>:
Once Bean class is instantiated using above statement, you have to use jsp:setProperty and jsp:getProperty actions to use the bean’s parameters. we will see both setProperty and getProperty after this action tag.
EmployeeBeanTest.jsp
<html> <head> <title>JSP Page to show use of useBean action</title> </head> <body> <h1>Demo: Action</h1> <jsp:useBean id="student" class="javabeansample.StuBean"/> <jsp:setProperty name="student" property="*"/> <h1> name:<jsp:getProperty name="student" property="name"/><br> empno:<jsp:getProperty name="student" property="rollno"/><br> </h1> </body> </html>
StudentBean.java
package javabeansample; public class StuBean { public StuBean() { } private String name; private int rollno; public void setName(String name) { this.name=name; } public String getName() { return name; } public void setRollno(int rollno) { this.rollno=rollno; } public int getRollno() { return rollno; } }
5. <jsp:setProperty> Action
This action tag is used to set the property of a Bean, while using this action tag, you may need to specify the Bean’s unique name (it is nothing but the id value of useBean action tag).
syntax of <jsp:setProperty>
<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name" /> .... .... <jsp:setProperty name="unique_name_to_identify_bean" property="property_name" />
OR
<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name"> .... .... <jsp:setProperty name="unique_name_to_identify_bean" property="property_name" /> </jsp:useBean>
In property_name, you can also use ‘*’, which means any request parameter which matches to the Bean’s property will be passed to the corresponding setter method.
6. <jsp:getProperty> Action
It is used to retrieve or fetch the value of Bean’s property.
syntax of <jsp:getProperty>
<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name" /> .... .... <jsp:getProperty name="unique_name_to_identify_bean" property="property_name" />
OR
<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name"> .... .... <jsp:getProperty name="unique_name_to_identify_bean" property="property_name" /> </jsp:useBean>
Other Action Tags
The below action tags are not frequently used so I haven’t covered them in detail.
7. <jsp:plugin> Action
This tag is used when there is a need of a plugin to run a Bean class or an Applet.
http://docs.oracle.com/javase/7/docs/technotes/guides/plugin/developer_guide/jsp.html
Leave a Reply