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.)
StringHttpServletRequest.getHeader("referer") Should give the URL of the previous page.
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Jan 15, 2001
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?
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.
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.
subject: how to find out the page the user was previously at.