This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes URLs and RequestDispatcher.forward() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "URLs and RequestDispatcher.forward()" Watch "URLs and RequestDispatcher.forward()" New topic
Author

URLs and RequestDispatcher.forward()

Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 192
Fellow Ranchers-

I have a question regarding URLs after using RequstDispatcher.forward().

I have an HTML form in a login.jsp file with an action of LoginServlet. The relevant snippet of code from the LoginServlet is below.



The URL before the login is: http://localhost:8080/SCWCD/login.jsp
The URL after a login (regardless of failure or success) is: http://localhost:8080/SCWCD/LoginServlet

Why doesn't the URL change to login.jsp or success.jsp? How can I make the URL change?

Thanks,
Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

Since you are using RequestDispatcher, so the URL will not change. If you want to change the URL in the browser, then use response.sendRedirect()...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 192
Thank you Ankit. Is there anything special about using response.sendRedirect() that I should be aware of? Is it the same a the RequestDispatcher except with the different URL or are there other things about it that I should be aware of?

Josh
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

When you use sendRedirect, then the browser sends a new request to the server. So like request dispatcher, you can't use request attributes. If you dispatch a request to a JSP, then the request attributes set by the servlet are maintained but with send redirect, any request attribute set by a servlet will not be maintained (this is logical as there is a new request so request attributes will be gone)...
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 163

You can also check the FAQredirect vs forward.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 192
Thank you. That's very helpful. I think I'll write some code to verify to myself that I understand it.

Thanks!
Josh
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: URLs and RequestDispatcher.forward()
 
Similar Threads
Unable to access second row in resultset
Forward pages with Servlets
Servlet not dircting to the next page
validation and forward
How to show the jsp Url while forwarding from a servlet