1

I have two JSP pages. In first page I have given fields to fill personal details and I have written request.getRequestDispatcher("second.jsp") and forwarded the the request. But When I run the "first.jsp" on server in eclipse, it is directly going to "second.jsp" but in URL it is shopwing "first.jsp". What might be the problem?

First.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>
<br>
<h2>Enter Your Personal Details</h2>
<form action="personal.jsp" method="get">
<table>
<tr><td>Name:      </td><td>   <input type="text" name="name" /><br /><br /></td></tr>
<tr><td>Email-ID:     </td><td><input type="text" name="email" /><br /><br /></td></tr>
<tr><td>Date Of Birth:</td><td><input type="text" name="dob" /><br /><br /></td></tr>
<tr><td>Password: </td><td><input type="text" name="pass" /><br /><br /></td></tr>
<tr><td>Age: </td><td><input type="text" name="age" /><br /><br /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
<%!
String uname=null,pass=null,email=null; 
String age=null,dob = null;
%>
<%
uname= request.getParameter("name");
session.setAttribute("username",uname);
pass= request.getParameter("pass");
session.setAttribute("password",pass);
age = request.getParameter("age");
session.setAttribute("age",age);
email = request.getParameter("email");
session.setAttribute("email",email);
dob = request.getParameter("dob");
session.setAttribute("dob",dob);
response.sendRedirect("academic.jsp");
%>
</body>
</html>

Second.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>
<br>
<h2>Enter Your Academic Details</h2>
<form action="academic.jsp" method="get">
<table><tr><td>
MID:       </td><td> <input type="text" name="mid" /><br /><br /></td></tr>
<tr><td>Marks:      </td><td>   <input type="text" name="marks" /><br /><br /></td></tr>
<tr><td>Salary:     </td><td><input type="text" name="salary" /><br /><br /></td></tr>
<tr><td>Stream:</td><td><select name="stream"><option>Java</option><option>dotNET</option><option>Testing</option></select><br /><br /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
<%
out.println(session.getAttribute("name"));
%>
</body>
</html>
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Babanna Duggani
  • 737
  • 2
  • 12
  • 34

3 Answers3

1

You need to do response.sendRedirect() to make the effect in url.

request#forward 

Silently passes the control to your another resource,And happens on server side,browser doesn't know about it.

Forward():

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.

sendRedirect()

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.

Highlighting Luggis comment,that move your business logic to Controller and try to avoid scriplets too if possible.

Though,it is not recommended,If you want to change the URL and still want to access the data in second page,one possibility is that put data in session and access in second jsp.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    That might work if OP's not setting any request attributes. It will be better explain what's the meaning of this instead suggesting plain redirection to handle all the navigations – Luiggi Mendoza Sep 04 '13 at 15:11
  • 1
    I am not getting using response.sendRedirect() method. Actually after getting all personal details in "first.jsp" and then storing those details in session object and then I want to redirect to "second.jsp", to fetch some more details in "second.jsp". But When I run "first.jsp" it is immediately redirecting to "second.jsp". – Babanna Duggani Sep 04 '13 at 15:14
  • 1
    @BabannaDuggani still the data in the session exists,don't worry.You can access it. – Suresh Atta Sep 04 '13 at 15:17
  • 2
    @BabannaDuggani this is due to you having a direct call of `request.getRequestDispatcher("second.jsp").forward(request, response)` probably in a scriptlet (which is **very bad practice**). All your navigations should be handled in servlets or another controller classes. – Luiggi Mendoza Sep 04 '13 at 15:18
  • *one possibility is that put data in session and access in second jsp* if you will do that, remove the data from session after its consumption – Luiggi Mendoza Sep 06 '13 at 19:37
  • Exactly,remove those attributes after that usage. – Suresh Atta Sep 07 '13 at 03:09
1

The problem is generated because you have a direct call of forward method in a scriptlet, this might look like this

request.getRequestDispatcher("second.jsp").forward(request, response);

By your question edit, this is generating the problem:

response.sendRedirect("academic.jsp");

Note that using scriptlets is highly discouraged.

Make sure all your data processing and navigation is handled in a Servlet or another controller classes provided by a MVC framework like JSF managed beans or Spring MVC @Controller decorated classes.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

The actual problem lies in first.jsp line response.sendRedirect("academic.jsp"); which is inside a JSP Declaration and not JSP Scriptlet, as per the doc variables and methods in JSP declarations become declarations in the JSP page’s servlet class which explains why when you hit the first.jsp its getting redirected to another page without any action and as other suggested its not advisable to use JSP scriptlets, or declarations in your JSP.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38