• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLs and RequestDispatcher.forward()

 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()...
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also check the FAQredirect vs forward.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That's very helpful. I think I'll write some code to verify to myself that I understand it.

Thanks!
Josh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic