JSP Forward Tag

JSP Forward Tag & Param Tag:

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource. When this action is encountered on a JSP page the control gets transferred to the page mentioned in this action.

Syntax:

<jsp:forwardpage="URL of the another static, JSP OR Servlet page"/>

We can see some examples for the Forward Tag which can make things very much clear.

forward.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example of forward JSP</title>
</head>
<body>

<p> This is my initial page in forward.jsp </p>
 
<jsp:forward page = "show.jsp" />
 
</body>
</html>

Show.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
This is the page which will be displayed by using the forward action tag from forward.jsp
 
</body>
</html>

Now upon running the forward.jsp which inturn has been forwarded to the show.jsp we will get the output of the show.jsp as below.

Also this can be confirmed by the root element which has been run is : http://localhost:8080/MVP/forward.jsp

Forward

Now these are simple logic where we do to understand the concept.

Lets practice some useful stuff with parameters and then to the real time concepts.

So let’s change the above program to make use of <jsp:param> in the above stuff.

forward.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example of forward JSP</title>
</head>
<body>
<p> This is my initial page in forward.jsp </p>
 
<jsp:forward page = "show.jsp" >
<jsp:param name="userid" value="programmingtute" />
<jsp:param name="password" value="programming" />
</jsp:forward>
 
</body>
</html>

show.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
Userid used to login: <%=request.getParameter("userid") %><br>
Password used to log in: <%= request.getParameter("password") %><br>
 
</body>
</html>

So in the above program we have used directives and other action tags to get the hard coded value of userid and parameter used for logging in. We will see this detail by using a realtime logging in screen in the future when we discuss the Implicit objects.

And the output of the above will be as below:

Forward

Posted on July 25, 2014 in Java Server Pages

Share the Story

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top