| 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
|
|
|
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
|
|
|
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
|
 |
 |
|
|
subject: URLs and RequestDispatcher.forward()
|
|
|