2 Minutes To Code (β Version)

JSP - 2 minute tutorial

Forward action - How to forward to another webpage in same app

Jsp specification allows the webserver to forward the http request coming to a particular url to a different url in the same webapp.

When a request is forwarded, all parameters/attributes/headers in the implicit request object and all session attributes are available in the forwarded jsp/servlet.The forwarded resource can be specified using relative or absolute path.

The code below shows how to forward a request to another page forwardedPage.jsp in the same webapp using relative url.
<html>
	<body>

			This line will be ignored.
			<jsp:forward page="forwardedPage.jsp" />
		
	</body>
</html>

While forwarding to another page, additional parameters can be sent in the implicit request object using the tags shown below.

Code of landing page - landingPage.jsp:

<html>
	<body>

			This line will be ignored.
			<jsp:forward page="forwardedPage.jsp" >
				<jsp:param name="zone" value="jsp" />
			</jsp:forward>
		
	</body>
</html>

Code of forwarded page - forwardedPage.jsp

<html>
	<body>

		This text is from the forwarded page
		<br>
		Zone is : <%= request.getParameter("zone") %>
		
	</body>
</html>


After deploying landingPage.jsp and forwardedPage.jsp, hit the landingPage.jsp in your browser. The resulting webpage will contain only the response from forwardedPage.jsp as shown in the image below. Also notice, that the url in the browser will still point to the first url i.e. landingPage.jsp.

Also, have a look at forwarding http request in a servlet to a different url in the same webapp.