Search This Blog

Friday, 28 October 2011

Standard Actions in JSP!

0 comments
JSP STANDARD ACTIONS! 
Standard actions are special tags provided in JSP for performing some of the most common tasks in a JSP program, such as including content from other resources, forwarding requests to other resources and interacting with Java Beans. JSP containers process actions at request time.
Actions are delimited by <jsp:action> and </jsp:action> tags, where action is one of the standard actions defined in JSP as listed below:

<jsp:include> Action:
This action enables dynamic content to be included in a Java Server Page. It just copies the content of the resource into the JSP once, at JSP translation time. If the included resource changes between requests, the next request to the JSP containing the <jsp:include> action includes the new content of the resource.
There are two attributes for this action include for specifying more about this action. They are listed below:


Attribute
Description
page
Specifies the relative URI path of the resource to include. The resource must be part of the same web application.
flush
Specifies whether the buffer should be flushed after the “include” is performed. In JSP1.1. this attribute is required to be true.

Table: Attributes of ‘include’ action with their description
The following is a JSP program (include.jsp) that includes two HTML and one JSP resources to generate static and dynamic content of a web page as a response:
<? xml version = “1.0”>
<html>
<head><title> JSP demonstrating ‘include’ action </title>
</head>
<body>
<table>
<tr>
<td style = “background-color : black;”>
<jsp:include page = “banner.html” flush = “true” />
</td>
</tr>
<tr>
<td style = width : 160px”>
<jsp:include page = “toc.html” flush = “true” />
</td>
<td style = “vertical-align : top”>
<jsp:include page = clock.jsp” flush = “true” />
</td>
</tr>
</table>
</body>
</html>
<jsp:forward> Action:
This action enables a JSP to forward request processing to a different resource. Request processing by the original JSP terminates as soon as the JSP forwards the request. This action has only one attribute: page that specifies the relative URI of the resource (in the same web application) to which the request should be forwarded.
The following is a JSP (forward.jsp) file that gets the user input – first name and forwards it to another JSP (clock.jsp) that displays the name and a clock:
<?xml version = “1.0”>
<head> <title> Forward request JSP </title>
</head>
<body>
<%
String name = request.getParameter(“fname”);
if (name != null)
{
%>
<jsp:forward page = “clock.jsp”>
<jsp:param name=“date”
value =“<%= new.java.util.Date() %>” />

</jsp:forward>
<%
}
Else {
%>
<form action = “forward.jsp” method = “get”>
<p>Type your first name and press Submit</p>
<p> <input type = “text” name = “fname” />
<input type = “submit” value = “Submit” />
</p>
</form>
<%
}
%>
</body>
</html>
Program: Forward.jsp that gets the user name and forwards it to another JSP

<?xml version = “1.0”>
<head> <title> Processing a forward request </title>
</head>
<body>
<h1>
Hello <%= request.getParameter(“fname”) %> <br/>
Your request has been received and forwarded!
</h1>
<table style= “border : 6px outset;”>
<tr>
<td style= “background-color : black;”>
<h2>
<%= request.getParameter(“date”) %>
</h2>
</td>
</tr>
</table>
</body>
</html>
Program: Clock.jsp that gets the gets name and date and displays them

In the above JSP example forward.jsp, when the user enters his name and presses submit button, his request is processed to get the current date and time of the server using java.lang.Date() object. Then the date and time are forwarded to another JSP as a parameter using jsp:param and jsp:forward actions, where the date and time of the server are displayed along with the name of the user.
The jsp:param action specifies name/value pair of information that are passed to the <jsp:include>, <jsp:forward> and <jsp:plugin> actions. Every <jsp:param> action has two required attributes: name and value. If a <jsp:action> specifies a parameter that already exists, the new value for the parameter takes precedence over the original value. All values for that parameter can be obtained by using JSP implicit object request’s getParameterValues method, which returns an array of strings.

Leave a Reply