JavaEnterprise JavaJava Server Pages:

Java Server Pages:

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.



Introduction

What is JSP?

With JSP, fragments of Java code embedded into special HTML tags are presented to a JSP engine. The JSP engine automatically creates, compiles, and executes servlets to implement the behavior of the combined HTML and embedded Java code.

Previous Lessons

Previous lessons have explained the use of the following syntax elements:

  • Output comments
  • Hidden comments
  • Declarations
  • Expressions
  • Scriptlets
  • The include directive
  • The page directive

This lesson will discuss the <jsp.forward> tag.

What is the <jsp.forward> tag

The <jsp.forward> tag can be used to cause a client request to be forwarded to an HTML file, another JSP file, or a servlet for processing.

Whenever the <jsp.forward> tag is encountered by the JSP engine, the engine forwards the request that was sent to the JSP file to another file.  The JSP engine doesn’t process the remainder of the current JSP file.

The output buffer

Anything that has been written into the output buffer by the current JSP file will be lost.

There are two possible outcomes regarding the output buffer.  If the output from the compiled JSP file is buffered, the buffer is cleared before the request is forwarded.

If the output is not buffered (page directive with buffer=none), and if the buffer is not empty, the use of the <jsp.forward> tag will result in an IllegalStateException being thrown.

Syntax

The syntax of the <jsp.forward> tag is as follows:

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

The value of the page attribute can either be a String representing a relative URL, or a JSP expression that evaluates to a string representing a relative URL.

The relative URL

Here is what Sun has to say about a relative URL.
 

“The relative URL looks like a path – it cannot contain a protocol name, port number, or domain name. The URL can be absolute or relative to the current JSP file. If it is absolute (beginning with a /), the path is resolved by your Web or application server.”

Sample <jsp.forward> tags

The following unrelated samples of <jsp.forward> tags are used in the sample program discussed in the next section.
 

<jsp:forward page=”jsp007a.jsp” >

<jsp:forward page=”jsp007a.htm” >

I will discuss the behavior of these two examples in detail in the next section.

Sample JSP Pages

This set of sample JSP and HTML pages illustrates two levels of forwarding.  The first JSP page forwards the request to a second JSP page, clearing the output buffer in the process.

The second JSP page forwards the request to an HTML page, also clearing the output buffer in the process.

Finally, the contents of the HTML page are placed in the output buffer and sent back to the client that requested the first JSP page.

The first JSP file


Figure 1
shows the first JSP page in the sequence.  This page is contained in a file named jsp007.jsp.  I have highlighted the forwarding tag in this file using boldface.

I also highlighted a paragraph tag and its content in green, immediately ahead of the forwarding tag.

Significance of the green code

The significance of the green line of code is that even though it is placed in the output buffer before the forwarding action occurs, we will see later that it is never sent back to the client.  This is because the output buffer is cleared when the forwarding action occurs.

Significance of the red code

I also highlighted a paragraph tag and its content in red.  The significance of the red line of code is that it is never executed.  As soon as the forwarding tag is encountered, all remaining code in the current JSP page is ignored.

The second JSP file


Figure 2
shows the second JSP page in the sequence.  This page is contained in a file named jsp007a.jsp.  As in the Figure 1, I have highlighted the forwarding tag in boldface.

Output from green code is lost

The output produced by the two green statements is lost when the forwarding action occurs, because the output buffer is cleared at that point in time.

Red code is never executed

Similarly, the red statement is never executed because all remaining code in this file is ignored when the forwarding action in the previous line occurs.

The HTML file


Figure 3
shows the HTML page that is the last stop in this “leap frog” sequence.  This page is contained in a file named jsp007a.htm.

This is a very simple HTML page that places two lines of output in the output buffer to be sent back to the client who originally requested the JSP page shown above in
Figure 1
.

The output


Figure 4
shows a replica of the output that appears in the browser window. As you can see, this is the output that you would expect from the HTML file shown in Figure 3 above.

More importantly, as you can also see, none of the output produced by either of the JSP files appears in the browser window.  This is because the output buffer is cleared each time a forwarding action occurs.

JSP Syntax Summary

In this and previous lessons, I have discussed the following JSP syntax:

Output comments:
<!– comment [ <%= expression %> ] –>

Hidden comments:
<%– hidden comment –%>

Declarations:
<%! declarations %>

Expressions:
<%= expression %>

Scriptlets:
<% code fragment %>

Include Directive:
<%@ include file=”relativeURL” %>

Page Directive:
<%@ page
  [ language=”java” ]
  [ extends=”package.class” ]
  [ import= “{ package.class |
    package.* }, …” ]
  [ session=”true|false” ]
  [ buffer=”none|8kb|sizekb” ]
  [ autoFlush=”true|false” ]
  [ isThreadSafe=”true|false” ]
  [ info=”text” ]
  [ errorPage=”relativeURL” ]
  [ contentType=”mimeType [
    ;charset=characterSet ]” |
    “text/html ; charset=ISO-8859-1” ]
  [ isErrorPage=”true|false” ]
%>

jsp:forward tag:
<jsp:forward page=
     “{ relativeURL | <%= expression %> }” >
 
 

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without  express written permission from Richard Baldwin is prohibited. 

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories