File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How to make a JSP/HTML page to stay on itself when user clicks on back button on the browser? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to make a JSP/HTML page to stay on itself when user clicks on back button on the browser?" Watch "How to make a JSP/HTML page to stay on itself when user clicks on back button on the browser?" New topic
Author

How to make a JSP/HTML page to stay on itself when user clicks on back button on the browser?

Kaverappa Prabhakar
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
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).

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
you forget choice 4

4. Fix the backend so the user can go back
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

Following good practices and established patterns pervents most gnarly problems. Please read this article, paying particular attention to the PRG Pattern.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kaverappa Prabhakar
Ranch Hand

Joined: Aug 18, 2009
Posts: 43


Eric,

I am not clear what you mean by "Fix the backend so the user can go back" Would you please explain to me again.

Please note that going back is not an issue or problem. As stated in my questions in my first posting, I have two ways what I want to accomplish:

They are:

Approach 1:

When the user tries to go back from main.jsp using browser back button I want to display main.jsp again ( tprevent user going back to the page in history registry - in this case page1jsp) based on the conditional statement in the javascript of main.jsp shown below:


But the above conditional statement does not execute and hence it goes back to page1.jsp. This may be because the displayed main.jsp, after user uses the browser back button when in main.jsp, is a cached page(not built in the server)

If the conditional statement is removed meaning with just



then it stays on main.jsp.

Approach 2:

Alternatively, just disable the submit button on page1.jsp when user arrives on page2.jsp from page1.jsp In order to do that I need to know whether user came to page1.jsp from main.jsp or page2.jsp.

To explain more clearly, let us say user just started the application using main.jsp and enters the relevant html text element data and then clicks on submit button on the main.jsp it takes the user to pag1.jsp. Now when the user clicks on submit button on pag1.jsp it takes the user to page2.jsp. At this point let us say user uses the browser back button on page2.jsp, which takes user to pag1.jsp.

The point is that user can arrive on page1.jsp from main.jsp or page2.jsp. When in page1.jsp I need to know which way user came to page1.jsp. One way to know is that I can check in page1.jsp the lcCalcBean in the server to see whether the value of submit is "Approve" if so I know user came to page1 from page2.jsp. Unfortunately I can not do that because one can only execute JSP tag code in the JSP page only at JSP factory on the server (At the time of building a new page upon cetain action by the user like cliking on a button etc.) In other words java tags does not get executed on a loaded page from cache ( in this case page1.jsp is a cached page displayed from the history when user used browser back button on page2.jsp.

In this case 2 the question remains How one can know wether user arrived at pag1.jsp from main.jsp or page2.jsp. Unless I know this I cannot disable the Submit button on pag1.jsp.

I have only two days to resolve this problem. I hope I get a good response from some one to resolve issues listed in either of the two approaches mentioned above.

Bear,

Thanks for the suggestion I will learn about it for my new project. But for now it would be helpful to know how I can resolve the issue I have presented in approach 1 and approach 2 above.

Thanks,

Prabhakar
Kaverappa Prabhakar
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Hello,


Can any one give some idea about how to resolve approach 1 or 2 posted in my previous udpate. Management at my present job wants me to figure this out before I leave my present job. I've only a couple of days left - perhaps until Monday.

I hope to hear from someone soon.

Thanks,
Prabhakar
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

You have already had good advice from people who know what they are talking about. If you reject that advice you aren't likely to get any other advice. Good luck in your next job, hopefully you won't be stuck with implementing bad requirements there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a JSP/HTML page to stay on itself when user clicks on back button on the browser?