Workaround for browser refresh/back button clicks?
Steve Renard
Ranch Hand
Joined: Apr 27, 2007
Posts: 66
posted
0
I have a jsp from where I do database add operation using HTML forms. The request goes to a servlet, ejb and comes back with attributes.. Depending on the attribute values, I display alerts in the jsp. I have the attribute checking and alert display code inside a page load function. So the problem is when the browser refresh or back buttons are clicked, the alerts show up. I don't like it that way.. Is there a way to check and bypass browser button click events?? Please help..
If the user refreshes the page, the problem doesn't go away. Don't you want the error message to persist until the user fixes the problem or moves on to another page?
Imagine if the form is empty and user hits refresh, the alert still shows up.. Also there could be many options such as Add, update, delete in the jsp.. When refresh is clicked, alerts may be displayed at random.. I think that's wrong..Does that make sense? How about the back button? It should not display anything anyway for browser back button event.. Should it?
First of all, using alerts for error messages is about as 1995 as you can get. Rather, use conditionals in the server side code to show the error message(s) directly on the page.
Secondly, you should not try to solve problems with your application by trying to hobble the user's browser. Not only can you usually not do it gracefully, or at all, it's just bad user interface design.
If you want to show the error messages once and only once, you could do something like store the messages in the session, and once they are displayed on the page the first time, remove them. A subsequent refresh of the page will find no messages to display.
Can you explain me better with some examples for (i) conditionals in the server side code &(ii) store the messages in the session and remove them after first time display