To prevent a user from logging off in a Java Web project and use the Back button in the browser to return to the front page __html5

Source: Internet
Author: User
Tags stub java web

This is typically done in the case of a user logoff in a Java Web project:

Session (). setattribute ("CurrentUser", null);

Or

Session.removeattribute ("CurrentUser");
</pre></p><p> or <pre name= "code" class= "Java" >session.invalidate ();

Then redirect to the login page.

But in this case, after the user logs off to the login page, if the user points to the browser's "Back" button, you can return to the page before the logout, although the session has been emptied.

This is because the browser's fallback is using a local cache.

The following approach prevents the browser from using caching, thereby preventing this from occurring.

New Package Com.example.filter, where you create a new class

import java.io.IOException;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

Import Javax.servlet.http.HttpServletResponse;
		
	public class Nocachefilter implements filter{@Override public void Destroy () {//TODO auto-generated method stub @Override public void Dofilter (ServletRequest req, servletresponse Res, Filterchain chain) throws IOException, S
	    ervletexception {HttpServletResponse HSR = (httpservletresponse) res; Hsr.setheader ("Cache-control", "No-cache, No-store, must-revalidate");
	    HTTP 1.1. Hsr.setheader ("Pragma", "No-cache");
	    HTTP 1.0. Hsr.setdateheader ("Expires", 0);
	    Proxies.
		
	Chain.dofilter (req, res); @Override public void init (Filterconfig arg0) throws Servletexception {//TODO auto-generated method stub}} 
The SetHeader in the Dofilter method is to tell the browser not to use the local cache.

Then configure the Web.xml in the

<filter>
    <filter-name>noCacheFilter</filter-name>
    <filter-class> com.leon.filter.nocachefilter</filter-class>
  </filter>
  <filter-mapping>
    < filter-name>nocachefilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </ Filter-mapping>
Where *.jsp means that any JSP page is filtered using Nocachafilter, you can of course be flexible according to your own situation, only if you do not want to be cached page use.

Of course, I can't forget to add login verification to the page

<%
	//Permission Verification
	if (Session.getattribute ("CurrentUser") ==null) {
		response.sendredirect ("login.jsp");
		return;
	}
%>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.