Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JSP forward action tag

JSP forward action tag is used for forwarding a request to the another resource (It can be a JSP, static page such as html or Servlet). Request can be forwarded with or without parameter. In this tutorial we will see examples of  <jsp:forward> action tag.


Syntax: 

1) Forwarding along with parameters.

<jsp:forward page="display.jsp"> 
<jsp:param ... />
<jsp:param ... />
<jsp:param ... />
...
<jsp:param ... />
</jsp:forward>

2) Forwarding without parameters.

<jsp:forward page="Relative_URL_of_Page" />

Relative_URL_of_Page: If page is in the same directory where the main page resides then use page name itself as I did in the below examples.


JSP Forward Example 1 – without passing parameters

In this example we are having two JSP pages – index.jsp and display.jsp. We have used <jsp:forward> action tag in index.jsp for forwarding the request to display.jsp. Here we are not passing any parameters while using the action tag. In the next example we will pass the parameters as well to another resource.

index.jsp
<html> 
<head>
<title>JSP forward action tag example</title>
</head>
<body>
<p align="center">My main JSP page</p>
<jsp:forward page="display.jsp" />
</body>
</html>

display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
Hello this is a display.jsp Page
</body>
</html>

Output:
Below is the output of above cpde. It is basically the content of display.jsp, which clearly shows that index.jsp didn’t display as it forwarded the request to the display.jsp page.


JSP Forward Example 2 – with parameters

Here we are passing the parameters along with forward request. For passing parameters we are using <jsp:param> action tag. In this example we are passing 4 parameters along with forward and later we are displaying them on the forwarded page. In order to fetch the parameters on display.jsp page we are using getParameter method of request implicit object.

index.jsp
<html> 
<head>
<title>JSP forward example with parameters</title>
</head>
<body>
<jsp:forward page="display.jsp">
<jsp:param name="name" value="Chaitanya" />
<jsp:param name="site" value="BeginnersBook.com" />
<jsp:param name="tutorialname" value="jsp forward action" />
<jsp:param name="reqcamefrom" value="index.jsp" />
</jsp:forward>
</body>
</html>

display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
My name is: <%=request.getParameter("name")%><br>
Website: <%=request.getParameter("site")%><br>
Topic: <%=request.getParameter("tutorialname")%><br>
Forward Request came from the page: <%=request.getParameter("reqcamefrom")%>
</body>
</html>

Output:
 Above code directly displayed display.jsp page, which is displaying the parameters passed from index.jsp page.




This post first appeared on Java4you - Java Programming Tutorials, Examples,, please read the originial post: here

Share the post

JSP forward action tag

×

Subscribe to Java4you - Java Programming Tutorials, Examples,

Get updates delivered right to your inbox!

Thank you for your subscription

×