wood burning stoves 2.0*
The moose likes Servlets and the fly likes Can servlet detect user's clicked 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 "Can servlet detect user Watch "Can servlet detect user New topic
Author

Can servlet detect user's clicked "Back" button in browser?

Allan Wang
Greenhorn

Joined: Apr 23, 2001
Posts: 20
Hi guys:
I use servlet as a controller to show pages. If user click "Back" button several times, and execute an operation in the page, at that time the attributes in the session is for the page before clicking "Back". So the operation run with the wrong data. How can I deal with this condition? Can I detect user's click on "Back"?
Thanks for any suggestion.
allan
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If the problem is in the consistancy of the data stored in the session, I'd recommend not storing it in the session.
Trapping or otherwise handling the use of the back button in web pages doesn't have any easy answer. Essentially it doesnn't work.
I'm a person from the camp that believes you shuold put as little information on the session as possible. The session should only be used for data that is true for the entire life of the session, for example storing the name of the logged in user.
The exception to this is data that is true at that time for all pages that the user may be able to visit on the page. An example of this use is using the session for a shopping cart. If the user adds an item to a cart, then this item exists in the cart regardless of which page they visit, even if it is a page in their history where the object didn't orignally exist.
So, without having any idea of what you are trying to accomplish and based on my feelings on the session object, I'd be looking at tryigng to achitect the problem in a different way so that (for example) state is managed between the pages themselves rather than using the session.
Dave
Allan Wang
Greenhorn

Joined: Apr 23, 2001
Posts: 20
Hi David O'Meara,
Thank you very much for your suggestion. In fact, I'am also questioning the use of session. I think, as you said, I should give up using session to store such much data.
But if I don't save data in the session, should I have to use multithread to interact with every client? In every therd, not every session, I store the data. Thanks for further explanation.

allan
[This message has been edited by Allan Wang (edited October 09, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Allan Wang:
I use servlet as a controller to show pages. If user click "Back" button several times, and execute an operation in the page, at that time the attributes in the session is for the page before clicking "Back". So the operation run with the wrong data.
Your controller keeps track of state, as stored in the user's session, so why not just set up the controller such that it doesn't allow invalid states. In other words, if a user hits the back button and re-invokes the controller servlet, then it could declare invalid state based on what page it was called from (this can be a request parameter) and the user's session attributes. And given an invalid state, your controller can then take appropriate action.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yeah, thats something I've seen but never been brave enough to try: a global controller.
You set up a single servlet as a global controller for all requests (this is the view-model-controller pattern of course), since all requests go through the same 'page' and the pages aren't cached, the back button loses meaning.
Sounds like a lot of work to force a browser to do something it wasn't built for...
Battle stories anyone?
Dave.
Allan Wang
Greenhorn

Joined: Apr 23, 2001
Posts: 20
David O'Meara,
In my web application which is simple and only consists of 10 jsp pages, I only use one servlet as a global controller. I am a newbie to jsp/servlet world.
Now I want to re-design my application. I don't want to use session to store such much data which is state relevant. But I face a problem with data communication between servlet and jsp page. I can use hidden input form element to send request to servlet, but if I don't use session what is the good way to transfer parameter/values to jsp page? I just found a way like following code:
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/Index.jsp?name=value");
But if I have many parameter want to notify jsp page, I think this way is limited. Are there better ways?
Thanks in advance.
allan
Allan Wang
Greenhorn

Joined: Apr 23, 2001
Posts: 20
I just found the way to transfer data from servlet to jsp page: using request.setAttribute(). Thanks for all help.
allan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Allan Wang:
But if I have many parameter want to notify jsp page, I think this way is limited. Are there better ways?
Thanks in advance.
allan
Allan, based on the MVC approach, the way to pass values from your controller servlet to a JSP is to:
- instantiate a bean, set its properties, and store it in the request scope,
- use a RequestDispatcher to forward to the JSP
- call jsp:useBean and jsp:getProperty from within the JSP to access the bean

[This message has been edited by Miftah Khan (edited October 16, 2001).]
 
Consider Paul's rocket mass heater.
 
subject: Can servlet detect user's clicked "Back" button in browser?
 
Similar Threads
Showing the status of an operation
Showing the status of an operation
Regarding Back button
How to end session after LogOut in struts application
Login problem