I have four relevant
jsp pages (plus other jsps)namely main.jsp, controllerPage.jsp, page1.jsp approval.jsp, page2.jsp, page3.jsp and its code shown below in the code block.
The main.jsp has a submit button, when user clicks on it displays page.jsp via controllerPage.jsp. The pag1.jsp also has a submit button. Upon clicking on its button it displays page2.jsp. Page2.jsp has its own submit button and upon clicking on it displays page3.jsp. Page3.jsp has no Submit button.
When the user clicks on the browser back button when the user is on page3 it takes them back to pag2.jsp. Since I am using Request scope
Java Bean I do not want the user to go back to page2.jsp because java bean instance will be different at that point and as a result run time error is encountered if the user clicks on the submit button on page2.jsp again (after going back to page2.jsp by clicking on browser back button on page3.jsp)
In order to prevent this problem I used onunload function in the page3.jsp to take the user back to main.jsp. The code does take the user back to main.jsp instead of page2.jsp. When in the main.jsp, user can still click on browser back button again. In that case it takes the user back to page1.jsp (because of history registry). To prevent the user taking back to page3 when clicked on browser back button for the same reason explained above (when in main.jsp) and make the user stay on main.jsp I again used onunload function in the main.jsp. This does keep the user in the main.Jsp page. Although it did solve the problem it creates a different problem.
The different problem is that when the user starts a new session using the main.jsp and tries to submit page it stays on the main.jsp because onunload trigger fires upon clicking its Submit button. To prevent this problem I added conditional statement in the javascript of the main.jsp as follows:
The above conditional statement resolved the problem. But when the user gets to page1 and from there to page2.jsp from there to page3.jsp and tries to use the browser back button when in page3.jsp it does take the user back to main.jsp as expected behavior but when the tries to use the browser back button again in this same main.jsp it take the user to page1.jsp. I was expecting it will make the user stay on the main.jsp. I think this is because the conditional statement
does not get execute although the value of it is blank.
Here are the questions:
1. Why is that, the conditional statement in the main.jsp does not execute when the user attempts to use the browser back button on this page. Is this because browser user cached main.jsp page? Also notice that the onload function in the main.jsp does not get executed when the user arrived on this main jsp from main.jsp or from page3.jsp.
2. As an alternative solution to prevent user going back words, I thought of passing a parameter/value indicating browser back button was pressed/clicked by user. The question is How to pass a parameter form a displayed page to the previous page?
3. The other alternative solution is that instead of preventing the user going back to the previous page, just allow the user to go to the previous page but disable the Submit button in the previous page if arrived there using browser back button in the page ahead of it. In order to do that I need to know the answer to previous question (question 2).