• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

URLs and RequestDispatcher.forward()

 
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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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)...
 
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
 
Not so fast naughty spawn! I want you to know about
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic