Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | jsp:forward Action - JavaServer Pages (JSP) Standard Actions

Chapter: Internet & World Wide Web HOW TO PROGRAM - JavaServer Pages (JSP): Bonus for Java™ Developers

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

jsp:forward Action - JavaServer Pages (JSP) Standard Actions

Request processing by the original JSP terminates as soon as the JSP forwards the re-quest.

Standard Actions

 

We continue our JSP discussion with the JSP standard actions (Fig. 31.5). These actions provide JSP implementors with access to several of the most common tasks performed in a JSP, such as including content from other resources, forwarding requests to other resourc-es and interacting with JavaBeans. JSP containers process actions at request time. Actions are delimited by <jsp:action>and </jsp:action>, where action is the standard action name. In cases where nothing appears between the starting and ending tags, the XML emp-ty element syntax <jsp:action /> can be used. Figure 31.5 summarizes the JSP standard actions. We use the actions in the next several subsections.



 

2. <jsp:forward> Action

 

Action <jsp:forward> enables a JSP to forward request processing to a different re-source. Request processing by the original JSP terminates as soon as the JSP forwards the re-quest. Action <jsp:forward> has only a page attribute that specifies the relative URI of the resource (in the same Web application) to which the request should be forwarded.

JavaServer Page forward1.jsp (Fig. 31.11) is a modified version of wel-come.jsp (Fig. 31.4). The primary difference is in lines 22–25 in which JavaServer Page forward1.jsp forwards the request to JavaServer Page forward2.jsp (Fig. 31.12).Notice the <jsp:param> action in lines 23–24. This action adds a request parameter rep-resenting the date and time at which the initial request was received to the request object that is forwarded to forward2.jsp.

 

      <?xml version = "1.0"?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <!-- Fig. 10.11: forward1.jsp -->

 

    <html xmlns = "http://www.w3.org/1999/xhtml">

 

      <head>

        <title>Forward request to another JSP</title>

        </head>

        <body>

        <% // begin scriptlet

        String name = request.getParameter( "firstName" );

         

        if ( name != null ) {

         

        %> <%-- end scriptlet to insert fixed template data --%>

         

        <jsp:forward page = "forward2.jsp">

        <jsp:param name = "date"

        value = "<%= new java.util.Date() %>" />

        </jsp:forward>

         

        <% // continue scriptlet

 

               }  // end if

               else {

                         

               %> <%-- end scriptlet to insert fixed template data        --%>

                         

               <form action = "forward1.jsp" method = "get">  

               <p>Type your first name and press Submit</p>

                         

               <p><input type = "text" name = "firstName"       />

               <input type = "submit" value = "Submit" />

         

                          </p>   

                          </form>          

                                     

               <%  // continue scriptlet     

                                     

               }          // end else     

                                     

               %> <%-- end scriptlet --%>

               </body>

         

               </html>          <!-- end XHTML document -->


        Fig. 31.11 JSP forward1.jsp receives a firstName parameter, adds a date to the request parameters and forwards the request toforward2.jsp for further processing

 

 

The <jsp:param> action specifies name/value pairs 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:param> action specifies a parameter that already exists in the request, the new value for the parameter takes precedence over the original value. All values for that param-eter can be obtained by using the JSP implicit object request’s getParameter-Values method, which returns an array ofStrings.

 

JSP forward2.jsp uses the name specified in the <jsp:param> action ("date") to obtain the date and time. It also uses the firstName parameter originally passed to forward1.jsp to obtain the user’s first name. JSP expressions in Fig. 31.12 (lines 23 and 31) insert the request parameter values in the response to the client. The screen capture in Fig. 31.11 shows the initial interaction with the client. The screen capture in Fig. 31.12 shows the results returned to the client after the request was forwarded to forward2.jsp.

 

    <?xml version = "1.0"?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

    <!-- forward2.jsp -->

 

<html xmlns = "http://www.w3.org/1999/xhtml"v

 

      <head>

        <title>Processing a forwarded request</title>

 

        <style type = "text/css">

.big {

             font-family:   tahoma, helvetica, arial, sans-serif;

             font-weight:  bold;

        font-size: 2em;

        }

        </style>

        </head>

        <body>

        <p class = "big">

        Hello <%= request.getParameter( "firstName" ) %><br />

        Your request was received <br /> and forwarded at

        </p>

        <table style = "border: 6px outset;">

<tr>

   <td style = "background-color: black;">

<p class = "big" style = "color: cyan;">

<%= request.getParameter( "date" ) %>

</p>

</td>

</tr>

</table>

</body>

</html>



Fig. 31.12 JSP forward2.jsp receives a request (from forward1.jsp in this example) and uses the request parameters as part of the response to the client

 

To test Fig. 31.11 and Fig. 31.12 in Tomcat, copy forward1.jsp and forward2.jsp into the jsp directory created in Section 31.3. Open your Web browser and enter the following URL to test welcome.jsp:

 

http://localhost:8080/advjhtp1/jsp/forward1.jsp

 

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.