Pin It

How to forward a request from one Jsp to another Jsp ?.

A simple Web-Application demonstrating how to forward a request from one jsp page to another jsp.

1. Create a JSP : index.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

<%@ 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>Login Page !!!!</title>
</head>
<body>
 <jsp:forward page="/welcome.jsp">
  <jsp:param name="firstName" value="Enter First Nam" />
  <jsp:param name="lastName" value="Enter Last Name" />
 </jsp:forward>
 <h1>This wont print as control forwards to welcome.jsp page</h1>
</body>
</html>

2. Create a JSP : welcome.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

<%@ 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>A User Input Page !!!</title>
</head>
<body bgcolor="#ADB1A9">

 <form method="POST" action="xxx.jsp">

  First Name : <input type="text" id="firstName"
   value='<%=request.getParameter("firstName")%>' /><br> 
    Last Name : <input type="text" id="lastName"
   value='<%=request.getParameter("lastName")%>' /><br> <br>

  <input type="submit" value="Submit" />
 </form>

</body>
</html>

Output of the program : 

As soon as user request for index.jsp page, the request goes to index.jsp and there it finds that it is been forwarded to welcome.jsp by the use of jsp:forward tag along with the necessary parameters with it. The control internally forwards to welcome.jsp and the rendered page of welcome.jsp is visible at the browser. Note : As soon as forward request comes the execution of calling JSP page gets stopped and request gets forwarded to another JSP. Here output shows that the last H1 tag doesn't gets outputted on the browser. 





Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot