This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to find out the page the user was previously at. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to find out the page the user was previously at." Watch "how to find out the page the user was previously at." New topic
Author

how to find out the page the user was previously at.

Josh Johnson
Greenhorn

Joined: Jan 15, 2001
Posts: 20
In a servlet, is there a way to find out the url of the page the user was previously at? For example, if a user is at http://www.orange.com/navel.jsp, and they click on a link in that page that executes a servlet, how can the servlet find out that the user was just at http://www.orange.com/navel.jsp? (Let's assume this servlet is pointed to by multiple jsp pages on the site, and it can't assume that it is always being called from navel.jsp.)
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
The method

String HttpServletRequest.getHeader("referer")
Should give the URL of the previous page.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Josh Johnson
Greenhorn

Joined: Jan 15, 2001
Posts: 20
Thanks for the help.
Unfortunately, that didn't work for me. request.getHeader("referer") returns null. I called request.getHeaderNames to look at the list of header names and referer was not listed. But I think that it should be. Isn't that a standard http request header?
I can only think of two places where the problem may be.
1) Jrun: jrun is my servlet engine and maybe it's getHeader method is not giving me access to the referer header. I don't know why jrun would let me see other headers but not referer.
2) Internet Explorer 5: IE is the browser I am using. Maybe it is simply not including the referer header in its request.
Any ideas?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This will work if the browser is set up to send such stuff. But not all are.
Better solutions, if you are really concerned about this, are to either encode a value in the calling URL
<a href='/controller?from=navel'>do it</a>
or put something in the session from the JSP which the servlet can query.
The best solution, however, is to design your application so this is not required. Make calls to your servlet effectively "modeless", containing all the information they need to generate the next step.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Josh Johnson
Greenhorn

Joined: Jan 15, 2001
Posts: 20
Tony's solution is working for me after all.
I goofed. I wrote a little test servlet to show me the value of referer. I would access that servlet my typing the url for the servlet directly into the browser. I was not getting to the servlet by clicking on a link in another page. So the reason request.getHeader("referer") was returning null was because there was no refering url to return!!
Thanks for your help Tony and Frank.
 
Don't get me started about those stupid light bulbs.
 
subject: how to find out the page the user was previously at.
 
Similar Threads
is it friday?
firmware id sony ericsson K 700i
Servlet Compilation Error
query strings
Deleting Cookies