Why after logout clicking back button on the page displays previous page content?

£可爱£侵袭症+ 提交于 2019-11-27 08:47:25

Turns out that your browser is caching pages before you press the back button. The browser caching mechanism is designed so to minimize the server access time by getting the page from the local cache if the page have the same URL. It significantly reduces the server load when browsing the server by thousands of clients and seems to be very helpful. But in some cases, especially in yours the content should be updated. The back button is designed so it caches every page that a user is browsing and retrieve them from the local cache when pressed the back button. So, the solution is to tell the browser to not allow caching when returning a response with a special headers that control the browser caching. In the servlet environment you might use a filter to turn off caching but in Struts2 you could use a custom interceptor. For example

public class CacheInterceptor implements Interceptor {

private static final long serialVersionUID = 1L;

@Override
public void destroy() {}

@Override
public void init() {}

@Override
public String intercept(ActionInvocation invoication) throws Exception {
    HttpServletRessponse response = ServletActionContext.getResponse();
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "-1");
    return invoication.invoke();
}

}

Now you could configure this interceptor to use by every action

<package name="default" extends="struts-default" abstract="true">

    <interceptors>
      <interceptor name="cache" class="org.yourcompany.struts.interceptor.CacheInterceptor "/>
      <interceptor-stack name="cacheStack">
        <interceptor-ref name="cache"/>
        <interceptor-ref name="defaultStack"/>
      </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="cacheStack"/>

</package>

When your packages extend default package they inherit the interceptor and the interceptor stack, you can also override this configuration by the action configuration.

Andrea Ligios

It happens because your browser cached the page on the client.

The solution is to prevent the caching of that page(s), by forcing the browser to request a new page even when pressing Back button, instead of reading the saved one.

Must read:

I don't really know struts. But an attempt could be to deactivate caching on sites where you need to be logged in.

You could also save a cookie or something else in your logout function and the main page checks with AJAX whether this cookie is set. And if it is you could reload the page with JS (and of course unset the cookie then).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!