File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disable back button

 
Ajay Singh
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers

I am currently making a web based application in jsp.I have a login page which asks for username and password.After the authentication it goes to the application stuff,there i click the logout button it comes back to the login page.Now when i click the back button of the browser it goes back to the previous page.I dont want this to happen.I was using window.history.forward(1); to disable the back button.Is there any other better way so as to prevent the login page to go back to application page?Any help would be appreciated.

Thanks
Umadas
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you diable browser side page caching using the pragma and cache control headers ? That way the browsers will not display the page and will instead display a warning message
 
Ajay Singh
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cant we do it using session or some kind of timer in the jsp page?
 
Prabhakaran Manoharan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Use the following script code in the page you want to restrict the user to come back.

<script language="JavaScript" type="text/javascript">
window.history.forward(1);
</script>
[ May 19, 2008: Message edited by: Prabhakaran Manoharan ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhakaran,

Please take a moment to read the original poster's question before responding. Ajay has stated that he already using this code and is looking for alternatives to it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't disable a browser's back button, nor should you try.

Instead look at two entries in our JSP FAQ.
One explains the post-redirect-get pattern which insures that no pages are rendered as a direct result of post request.

The other explains how to use no-cache headers to request that the browser not keep copies of pages on the disk.

-Ben
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajay,

I too have come across the same situation. But in my scenario, I have set the userid, password, etc., in session and while logging off, I had invalidated the session. After you logoff, click on the back button, you don't get the necessary info in the jsp page, resulted in an exception (I made the code accordingly), then redirected to the homepage.

Regards,
Kiran.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K Kiran Kumar:
Hi Ajay,

I too have come across the same situation. But in my scenario, I have set the userid, password, etc., in session and while logging off, I had invalidated the session. After you logoff, click on the back button, you don't get the necessary info in the jsp page, resulted in an exception (I made the code accordingly), then redirected to the homepage.

Regards,
Kiran.


With the scenario in question the browser actually caches the page (at least that is what it looks like) and displays it to the user. It does not go back to the server and get a page in response
 
Usman Saeed
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree with Kiran Kumar. When user logoff, user's session should be killed, in this case when user'll click on back button, session not found error will come.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second of two options what Ben suggested is the simple one and easy way to get rid of this.

Try include the following code snippet in your Jsp page.



It works perfectly!
 
pavan mandala
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly raghavan..setting response headers like that will serve the purpose
 
chris welz
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not work in Firefox!!
Also it's not a very usable behavior that you will get an "Page expired" Error Message.

There must be a better solution?

chris
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"chriswelz",
Please check your private messages about an important administrative matter.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chriswelz:
This will not work in Firefox!!
Also it's not a very usable behavior that you will get an "Page expired" Error Message.

There must be a better solution?

chris


I disagree - I would say 80% of the pages I have use no-cache headers. I use Firefox as my main test-bed and never have seen a problem. A page past its cache-life will be re-requested from the server.

On the otherhand, if you are using the no-cache headers WITHOUT implementing the Post-Redirect-Get pattern Ben mentions, you could get this warning when the user tries to go back to the page the form submits to. When you implement the Post-Redirect-Get pattern then you never get to that page, because the redirect-get replaces the Post request in the browser.

So if you are getting the error it is probably because you have not done both things Ben had suggested.
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disabling the browser's back button is evil.

Your users expect it to work.

That you can hack things to work most of the time on most of the browsers does not make it a good thing to do.

What you want is to have a great, intuitave user experience.

Fix your code to work properly with back and forward as Marc Andreesen intended.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed! Disabling the back button is a crutch to get around coding your web application properly. Fix the web app, don't try to break the browser.

Please read this for more information.
[ October 17, 2008: Message edited by: Bear Bibeault ]
 
Yogesh Lonkar
Ranch Hand
Posts: 94
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote:The second of two options what Ben suggested is the simple one and easy way to get rid of this.

Try include the following code snippet in your Jsp page.



It works perfectly!


Thanks works perfectly fine
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic