• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting url not working as expected

 
Kyle Bradley
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to store the url visited so when the session does not exist it redirects you to the login page then after login it will direct you to the page you were attempting to access.

Setup:
web.xml directs /action to servlet action.java

I access the page from the servlet(action.java) and redirect you to the jsp page via: request.getRequestDispatcher("/view.jsp").forward(request, response);
The url says the servelts name

So on the page action the url is:
localhost:8080/App/action

but request.getRequestURI().toString(); shows /App/view.jsp


How can I get the url to show the servlet address?


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A forward happens only on the server, so the browser knows nothing about it.

If you want the browser to fetch the new page, use a redirect, not a forward.
 
Kyle Bradley
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are saying seems backwards then... I want the url of the servlet which would not be the forwarded url.
 
Kyle Bradley
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe I didnt explain it well enough...


If I do this I get the result I want:



Result gives me the url I want
Localhost:8080/App/action?whateverParametersIHaveHere


What I did not want to have to do was the check (parentPage.equals("/App/view.jsp"))

Is there not a way to get the actually url in the url bar? I thought that request.getRequestURL would give it to you but it is returning the forwarded url of the .jsp (I do not want the .jsp but the servlet address that is in the url bar)

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you would think that.

In any case, if I recall correctly, the container places attributes in the request that contain the original url information. If you carefully inspect the attributes (or look it up in the Servlet specification) you will likely find what you seek.
 
Christian Taylor
Greenhorn
Posts: 8
1
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the container already handle all of this for you? If a page that requires a login is accessed, the request will be intercepted and redirected to the login page, and then redirected back to the original URL after logging in. This assumes that the standard and well-tested container-based security is being used, and you aren't trying to create your own login system for some reason.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Taylor wrote:This assumes that the standard and well-tested container-based security is being used ...

Facts not in evidence.
 
Nandareddy Poli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the context path and then point the servlet path according to url pattern of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic