JSP - SPLessons

JSP Standard  Actions

Home > Lesson > Chapter 29
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Standard  Actions

JSP Standard Actions

shape Description

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

shape Description

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

shape Example

JSP Standard  Actions - Following is an example.  index.jsp [html] <html> <head> <title>JSP forward action tag example</title> </head> <body> My main JSP page <jsp:forward page="printdata.jsp" /> </body> </html> [/html] Here the developer just forward tag for the page printdata.jsp .  printdata.jsp [java] <html> <head><title>Display Page</title></head> <body> Hello this is a display.jsp Page </body> </html> [/java] 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

shape Example

JSP Standard  Actions - Following is an example.  farwithpar.jsp [java] <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> [/java] Here forward tag used on display.jsp page, parameters are passing here. display.jsp [java]<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> [/java] The request.getParameter() is used to retrieve the parameters. Output Now compile the code result will be as follows.

JSP:include

shape Description

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>  

shape Example

includetag.jsp [java] <html> <body> <h2>Hi</h2> <h2><jsp:include page="printdata.jsp"/></h2> </body> </html> [/java] Here printdata.jsp page has been included. printdata.jsp [java] <html> <head><title>Display Page</title></head> <body> Hello this is a display.jsp Page </body> </html> [/java] 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

shape Key Points

  • 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