9.1 Overview of JSP Actions
JSP specification provides several standard action tags intended for a specific tasks such including other resource or forwarding the request to other resources or working with java bean objects etc.
General syntax of a jsp action is <jsp:action_name attribute="attribute_value" />
Below are the jsp actions –
• <jsp:include>
• <jsp:forward>
• <jsp:plugin>
• <jsp:text>
• <jsp:useBean>
• <jsp:getProperty>
• <jsp:setProperty>
• <jsp:element>
• <jsp:attribute>
• <jsp:body>
9.2 <jsp:include>
This action tag is used to include any other resource (may be html, jsp etc) at a runtime. I am again repeating “at runtime” which means at the time when page is requested.
Do you remember we discussed include directive as well in last chapter? Yes we did and include directive also include other resources but the difference between include directive and action is , action includes resource at run time where as directive include at translation time.
General syntax of include looks like -
<jsp:include page="another file" />
If additional parameters need to pass to the including file we need to use <jsp:param> tags which takes two attributes name and value like
<jsp:include page="another file" > <jsp:param name=”param_name” value=”param value “/> </jsp:include>
We can use multiple <jsp:param> tags and params will be available as request parameters in the included resource.
Lets take an example in which main.jsp file will include displayIP.jsp file
a) main.jsp code
<html> <head> <title> Main Page </title> </head> <body> <H4> This is main page and to explain include directive </H4> <br/> <jsp:include page="displayIP.jsp" /> </body> </html>
b) displayIP.jsp
<html> <head> <title> Display IP address </title> </head> <body> <% String ipAddress = request.getLocalAddr(); out.println("Client IP address is " +ipAddress ); out.println("<br/>"); %> </body> </html>
c) Access main.jsp using http://localhost:8080/jsp-tutorial/main.jsp
Now pass two parameters from main.jsp to displayIP.jsp
Updated main.jsp code
<html> <head> <title> Main Page </title> </head> <body> <H4> This is main page and to explain include directive </H4> <br/> <jsp:include page="displayIP.jsp" > <jsp:param name="param1" value="value1"/> <jsp:param name="param2" value="value2"/> </jsp:include> </body> </html>
Updated displayIP.jsp code
<html> <head> <title> Display IP address </title> </head> <body> <% String ipAddress = request.getLocalAddr(); out.println("Client IP address is " +ipAddress ); out.println("<br/>"); out.println("Param1 :: " + request.getParameter("param1") ); out.println("<br/>"); out.println("Param2 :: " + request.getParameter("param2") ); out.println("<br/>"); %> </body> </html>
9.3 <jsp:forward>
The action tag jsp:forward is used to forward request any other resource (may be html, jsp etc).
General syntax of include looks like -
<jsp:forward page="another file" />
If additional parameters need to pass to the including file we need to use <jsp:param> tags which takes two attributes name and value like
<jsp:forward page="another file" > <jsp:param name=”param_name” value=”param value “/> </jsp:forward>
We can use multiple <jsp:param> tags and params will be available as request parameters in the included resource.
Lets modify the example taken in section 9.2
a) Modify main.jsp code to change include action to forward action
<html> <head> <title> Main Page </title> </head> <body> <H4> This is main page and to explain include directive </H4> <br/> <jsp:forward page="displayIP.jsp" > <jsp:param name="param1" value="value1"/> <jsp:param name="param2" value="value2"/> </jsp:forward> </body> </html>
b) displayIP.jsp will remain same
<html> <head> <title> Display IP address </title> </head> <body> <% String ipAddress = request.getLocalAddr(); out.println("Client IP address is " +ipAddress ); out.println("<br/>"); %> </body> </html>
c) Access main.jsp using http://localhost:8080/jsp-tutorial/main.jsp. With forward tag request is forwarded and that is the reason only displayIP.jsp code is displayed and main.jsp output is not included (refer below figure)
9.4 <jsp:plugin>
The action tag <jsp:plugin> is used to embed the applets or any java bean and generates browser-dependent HTML code (OBJECT or EMBED) that displays and executes a Java Plugin software (Java applet or a JavaBean component) in the current JSP page. In case plugin does not found, it downloads the required plugin and runs the applet or a java bean.
The <jsp:params> and <jsp:fallback> are optional sub elements which can be used to pass parameters or display message to user in case of error to user respectively.<jsp-paramsmust contain at least one <jsp:param> tag.
Syntax of <jsp:plugin> tag is
<jsp:plugin type="bean|applet" code="objectCode" codebase="objectCodebase" {optional attributes} { <jsp:params> { <jsp:param name="paramName" value="paramValue" /> }+ </jsp:params> } { <jsp:fallback> arbitrary_text </jsp:fallback> } </jsp:plugin>
Sample plugin example
<html> <head> <title>JSP Plugin </title> </head> <body> <jsp:plugin type="applet" code="com.sample.jsp.tutorial.SampleApplet.class" codebase="html"> <jsp:params> jsp:param name="message" value="This text will be passed to applet " /> </jsp:params> <jsp:fallback> <p>Could not load applet!</p> </jsp:fallback> </jsp:plugin> </body> </html>
9.5 <jsp:text>
The action tag <jsp:text> used to write template text in JSP pages or JSP Documents (JSP documents are xml representation of jsp which we will cover in coming chapters) and primarily used to write html code in writing jsp documents. This tag does not have any attributes.
Syntax for this action:
<jsp:text>Template text</jsp:text>
But if template text contains other characters like < or > (for example if text is <strong>template text<strong> )then we need to include the template text in CDATA section like below
<jsp:text><![CDATA[<strong>template text<strong>]]></jsp:text>
For example create a text.jsp with below content in WebContent directory with below content
<html> <head> <title> Text JSP </title> </head> <body> <jsp:text>Template text</jsp:text> <br/> <jsp:text><![CDATA[<strong>template text<strong>]]></jsp:text> </body> </html>
Access text.jp with http://localhost:8080/jsp-tutorial/text.jsp url