This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
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: 193
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
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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: 193
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
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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: 171

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

Joined: Aug 22, 2005
Posts: 193
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://aspose.com/file-tools
 
subject: URLs and RequestDispatcher.forward()