Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I redirect to a JSP page from a servlet w/o losing the variable

Status
Not open for further replies.

VCNewbie

Programmer
Dec 5, 2001
8
PH
Hi!

I have 2 jsp pages page1.jsp and page2.jsp and 1 servlet, on the form action of page1 I called the servlet setting certain parameters (i.e. name), on the doPost method of my servlet, I called requestDispatcher to load page2.jsp. On page2.jsp I want to retrieve the value from page1 (for example: String s = request.getParameter(&quot;name&quot;);) My problem is that when i click the submit button of page1, the url of the new page is /servlet/<servletnae> instead of page2.jsp. When I tried to use sendRedirect I get a null pointer exception. It seems that I can't pass the parameter name from page 1 to page 2 even if in the doPost method of my servlet, I set req.setParameter(&quot;name&quot;,name); How can I correct this error?

Thanks!

Raymond
 
Get a RequestDispatcher in your Servlet and forward the request to the second JSP page.
 
I used RequestDispatcher in my Servlet and the parameters were passed correctly but in the location of the internet explorer, the url was even if the page that was displayed is page2.jsp.

I want the url of the location to be
Perhaps I should post my code to lessen confusion =)

doPost
{
location = &quot;page2.jsp&quot;
RequestDispatcher disp = req.getRequestDispatcher(location);
disp.forward(req, res); <- is this what you mean by forwarding it?
}

thanks!
 
That is exactly right. You are handing off the request to the jsp page without need for further client intervention. This is what the MVC architecture is all about. The url not changing is not a limitation, it is a benefit. The client shouldn't know or need to know what jsp page is ultimately servicing their request. In fact, in most J2EE Applications you will only see the servlet being accessed because it is single-entry point for the application and it proxies the request to the necessary places.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top