Menu
Share

The JSP Forward Action

The <jsp:forward/> action is used to permanently transfer processing from one JSP to another on the local server. Any content generated by the original page is discarded and the processing begins anew at the second JSP.

Syntax

<jsp:forward page="URL" />

<jsp:forward page="URL" >
  <jsp:param name="ParamName1" value="ParamValue1" />
  <jsp:param name="ParamName2" value="ParamValue2" />
</jsp:forward>

As you can see it is very similar to the <jsp:include/> action where we specify a page attribute which is the file we are forwarding to. Note, the browser is not notified when the request is forwarded to another JSP. Indeed the browser still displays the original request URL. The diagram below illustrates the behaviour of the <jsp:forward/> action.

Sequence diagram for forward action
Sequence diagram for the <jsp:forward/> action.

As with the <jsp:include/> action the <jsp:forward/> action also supports parameters and the use of JSP expressions in the body of the attribute values.

An alternative method for passing data from the first page to the forwarded page is to hold the data as attributes of the request object in the same manner as holding attributes for session and application objects that we saw earlier.

Given that the <jsp:forward/> action effectively terminates the processing of the current page this tag is typically used in conditional code. This is illustrated in the following example.

<%
  if(!strPassword.equals("password"))
  {
%>
    <jsp:forward page="Login.jsp">
      <jsp:param name="FailReason" value="Wrong Password"/>
    </jsp:forward>
<%
  }
%>

<H2>Welcome!</H2>

Notes

One factor that you need to keep in mind when using this tag is its interaction with output buffering. When the processing of a page encounters a <jsp:forward/> tag all out put generated so far will be cleared. If, however, some content has already been sent to browser (flushed) then an IllegalStateException will occur. To avoid this either ensure that the <jsp:forward/> tag occurs towards the top of the page or adjust the size of the buffer using the buffer directive,

<%@ page buffer="12kb" %>

Another important point touched on briefly earlier is the fact that the browser is not notified of the forwarding and in fact displays the original request URL. This can upset browser requests which to document relative URLS rather than using root relative URLs.

For example let us assume that we requested the following URL,

http://localhost/myJSPApp/Security/login.jsp

When the file "login.jsp" executes it performs the following <jsp:forward/> action,

<jsp:forward page="../Welcome/Welcome.jsp" />

This is a forward to another JSP in another folder "Welcome" which is at the same level as the "Security" folder. The browser URL will read,

http://localhost/myJSPApp/Security/login.jsp

Now this may not seem too much to worry about however if "Welcome.jsp" contains a Form whose action tag sepcifies "NewQuery.jsp",

<FORM name="QueryForm" action="NewQuery.jsp"

Then the browser will resolve this as a request to,

http://localhost/myJSPApp/Security/NewQuery.jsp

Which will fail with a "404 : File not Found" error since "NewQuery.jsp" is in the "Welcome" folder. What we want is the browser to request,

http://localhost/myJSPApp/Welcome/NewQuery.jsp

To avoid this we should specify an app relative URL in the FORM,

<FORM name="QueryForm" action="/myJSPApp/Welcome/NewQuery.jsp"
      method="POST">

A similar method to the <jsp:forward/> action is the response.sendRedirect() method. This method may look like forwarding except that instead of passing on the request it instructs the browser to redirect to another page. This is done by adding a redirect HTTP header. It is worth noting that the whole page is executed and the output sent to the browser. The new page will only be executed on receving the request from the browser. Obviously the disadvantage here is the extra round trip to the server.

The JSP Plugin Action

The <jsp:plugin/> action is used to generate browser specific HTML for specifying Java applets which rely on Sun's Java plugin which is different from using an ordinary applet.

Summary

This topic concentrated on the <jsp:include/> and <jsp:forward/> actions. We saw that the <jsp:include/> action could be used to include the output of another page. This included page could be a JSP, a Servlet or just a static HTML page.

The <jsp:forward/> action is similar in structure except that here the request was forwarded to the second page with the output from the first page discarded.

Workshop

The next section is a workshop on using the <jsp:forward> actions.