JSP - SPLessons

Chapter 29

JSP Standard  Actions

JSP Standard Actions

The JSP Standard Actions are used within the JSP page and are used to eliminate or remove scriptlet code in a JSP page because now-a-days, scriptlet tag is not recommended. The programmers consider these has a bad practice to put the Java code in the JSP page. The standard tags are as follows.


JSP: forward

The jsp: forward activity tag is utilized to forward the solicitation starting with one asset then onto the next resource.The different assets are JSP, HTML.

Syntax

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

//jsp:forwar tag with parameters

<jsp:forward page=“relativeURL | <%= expression %>”>  <jsp:param name=“parametername” value=“parametervalue | <%=expression%>” />

</jsp:forward>

JSP: forward tag without parameter

JSP Standard  Actions – Following is an example.

 index.jsp

<html> 
<head>
<title>JSP forward action tag example</title>
</head>
<body> 
My main JSP page
<jsp:forward page="printdata.jsp" /> 
</body> 
</html>
    

Here the developer just forward tag for the page printdata.jsp .
 printdata.jsp

<html>
<head><title>Display Page</title></head>
<body>
Hello this is a display.jsp Page
</body>
</html>

When compile the code from the index.jsp page printdata.jsp page text will be displayed.
Output
Now compile the code from index.jsp file, result will be as follows.

JSP: forward tag with parameter

JSP Standard  Actions – Following is an example.

 farwithpar.jsp

<html> 
		<head><title>JSP forward example with parameters</title></head>
			<body> 
				<jsp:forward page="display.jsp"> 
				<jsp:param name="name" value="Sai" /> 
				<jsp:param name="site" value="Splessons.com" /> 
				<jsp:param name="tutorialname" value="jsp forward action" /> 
				<jsp:param name="reqcamefrom" value="farwithpar.jsp" /> 
				</jsp:forward> 
			</body> 
</html>

Here forward tag used on display.jsp page, parameters are passing here.
display.jsp

<html>
	<head><title>Display Page</title></head>
<body>
<h2>Hello this is a display.jsp Page</h2>
My name is: <%=request.getParameter("name") %>
Website: <%=request.getParameter("site") %>
Topic: <%=request.getParameter("SPlesson") %>
Forward Request came from the page: <%=request.getParameter("reqcamefrom") %>
</body>
</html>

The request.getParameter() is used to retrieve the parameters.
Output
Now compile the code result will be as follows.

JSP:include

The jsp:include tag are used to include the content of the other resource like jsp, html, servlet, The include action tag are process or include the content at the request time, so jsp:included tag is utilized for element pages since it may change in future. Reusability of a code is an advantgae with include tag.

JSP include directive JSP include action
At interpretation time it incorporates different assets. At solicitation time it incorporate different assets.
It better to use static pages. It better to use dynamic pages.

Syntax

<jsp:include page=“relativeURL | <%= expression %>” />  

//jsp:include action tag with parameter

<jsp:include page=“relativeURL | <%= expression %>”>

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

</jsp:include>  

includetag.jsp

<html>
<body>
<h2>Hi</h2>
<h2><jsp:include page="printdata.jsp"/></h2>
</body>
</html>

Here printdata.jsp page has been included.
printdata.jsp

<html>
<head><title>Display Page</title></head>
<body>
Hello this is a display.jsp Page
</body>
</html>

The parameters have passed here.
Output
Now compile the code result will be as follows.

action tag redirect the servlet Request and Servlet Response to another resource specified in the tag. The path of the resource is ‘relative path’. The output generated by current JSP will be discarded.
action tag process the resource as part of the current jsp. It include the output generated by resource, which is specified in the Tag to the current JSP. In both the cases single Request process multiple resources.

Summary

  • JSP Standard Actions – Forward tag is used to forward the request and response to other  resources.
  • JSP Standard Actions – Include tag is used to insert another file like html, JSP, java