forward Action JSP Example jsp:forward

forward Action JSP Example jsp:forward


The JSP forward action is similar to JSP include but with a small difference. In include directive, output of both JSP files goes to client. But in forward action, client gets only one JSP file content. Client gets included file content only but not including file.

The JSP forward action sends client request to another JSP file while terminating the current file (actually which is called by client).

Example: <jsp:forward page="Marks.jsp" flush="true" />

The include action takes two attributes of page and flush.

1. page attribute takes relative path of included page, interpreted relative to the current JSP file. The include file can be static HTML file or dynamic JSP file.

2. flush is optional which takes a boolean value of true or false. If true, the response content is put into buffer before sent to client. Default is false.

In this example, the including file Alphabets.jsp comes with lowercase English letters and included file Letters.jsp comes with uppercase English letters. Only uppercase letters are sent to client. For simplicity, only static content is given in both the JSPs.

1. Including file, File Name: Alphabets.jsp

2. Included file, File Name: Letters.jsp

ima

Observe in the above screenshot, only ABCDEFGHI are seen of that of included file Letters.jsp. That is, including file data is not sent to client. But with jsp:include action, both files data is sent to client. Just change jsp:forward to jsp:include (and no other changes) in the above code and see the difference.

Rules to remembered with forward usage:

  1. The address of the JSP page or URL can be relative to the current JSP file or relative to the web applications’s context where URL starts with /).
  2. The JSP forward action terminates the main or current JSP file (in which jsp:forward is used) execution. If any response exists, it will not go to client. So, use the forward action only when some extraordinary cases occur while some exception occurs (to send exception to errorPage designated file) or when login fails (to call forward action to another JSP page that sends new Login form) etc.
  3. forward action is very transparent to client as client or browser does not know, it is getting response from a another URL (or another JSP file and not from that it has requested the server) as the prompt in the browser window does not change.
  4. The page attribute value can be taken at runtime also using JSP expression.
  5. As usual, the Web server throws 404 status code error when the forwarded URL is not found.

Note: If page buffer value is set to "none" in including JSP file, then buffering the data in included page raises IllegalStateException.

The differences between JSP include directive and JSP forward action are tabulated and given in 10 differences between include action and forward action in JSP

    Other Include and Forward Examples

  1. JSP include directive with Example and Screenshots
  2. JSP Standard Action tags – 10 List – Examples Screenshots
  3. JSP include action Example
  4. JSP include action with Parameter passing Example
  5. JSP forward Action jsp:forward with Example, Screenshot
  6. JSP forward action with Parameters Example

JSP View All

Leave a Reply

Your email address will not be published. Required fields are marked *