It's not a secret anymore!
The moose likes Servlets and the fly likes Back Button detection with Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Back Button detection with Servlet" Watch "Back Button detection with Servlet" New topic

Back Button detection with Servlet

Kelley Koszegi

Joined: Aug 20, 2001
Posts: 27
Looking through this group and others, I see that disabling the browser back button is not 100% possible. But is it possible for a servlet to somehow 'detect' that the back button was pressed? Are there any tricks to do this?
Just for background: the application I am working on will fail sometimes when using the back button because information is taken both from a link on the page and the session. When using the back button, the session data will become outdated and may not be compatible with the link parameters of the current page.
Any suggestions would be appreciated. TIA!

Kelley Koszegi<br />Sun Certified Java Programmer
Brian Blesbok

Joined: Sep 03, 2001
Posts: 9
you can use:
in the header of each and every servlet page - include it in a js file if you want... that way, when a user presses the back button, on trying to load the provious page, the browser goes forward one ie: stays on the current page.
In IE this works sweet, and there is no page flicker really -but NutScrape has a bit of a flicker - but both work.
This is the only way I have found of disabling the back.
Another way, is to code your app so that it handles the situation where someone has pressed the back button. always code for the worst case scenario -if a user can break it, they will - and complain they will also!!
Kelley Koszegi

Joined: Aug 20, 2001
Posts: 27
Thanks for your reply, Brian. Things have changed a little and disabling the back button is not a possibility.
Like I said before, I am interested in finding out how the servlet could detect that the back button was pressed. Does anyone have an idea as to how I could do that?
Kareem Gad
Ranch Hand

Joined: Aug 06, 2001
Posts: 89
To my knowledge its not actually possible to have some browser listener and deal with the events happening on the browser .. back or no back ..
its all client side.
Servlets are server side remember ?


<b><i>KaReEm</i><br /><ul type="square"><li>SCJP-Free Range Web Developer <br /></ul></b>
Steve Snodgrass

Joined: Jun 12, 2001
Posts: 27
We use javascript and hidden fields to detect the condition where a page is loaded from the browser cache as opposed to the server. If it is we pop up an alert informing the user that they should hit refresh to get the latest data or unexpected results may occur. Alternatively you could post a form to the servlet upon this condition so it is aware of the situation and handles it.
Our app has never been tested with Netscape so I am not sure if this would work.
I agree. Here's the link:
subject: Back Button detection with Servlet
It's not a secret anymore!