1

I am allowing user to log in in my JSP site. But when he gets logged in he/she gets redirected to the home page.

So I want him to remain on the same page instead of getting redirected to some specific location.

So I want to access the link of the source page which initiated login/logout request, so that I will logout the user and then redirect to the same page. I will also need to access the querystring.

I tried request.getRequestURL/URL, but they return the link that is used to reach this servlet (i.e. actually LoginHendlerServlet link itself) not the link of the page on which login button is clicked.

Is there any ready-made API method to access this one older link.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • Try looking at http://stackoverflow.com/questions/2648984/httpservletrequest-how-to-obtain-the-referring-url - is that what you're looking for? – Grim Feb 15 '13 at 21:41

1 Answers1

0

Here's how I do it:

Use a filter which intercepts all the pages that need authentication to be served. In this filter, if the request is a GET request, is not an AJAX request, and the user is not authenticated, store the requested URL with its query string in a request attribute, and forward to the login page.

The login page contains a hidden field with the request URL, which is submitted with the login form.

In the login servlet/action, if the requested URL request parameter is populated, once the login succeeds, redirect to this URL. Else, redirect to the login page.

You could also use the session to store the request URL, but I prefer keeping the session as empty as possible.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255