Restrict back button of browser using Struts2

|
| By Webner

Follow the steps below to restrict back button of the browser:

1. Make a custom filter class as shown below.
2. Implement Filter (in-built interface)
3. Override “doFilter” method present in the Filter interface:

public class NoCacheFilter implements Filter
{
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(req, res);
}

Add @WebFilter annotation to the above class.
In web.xml file (that resides in WebContent->WEB-INF) , add the below mapping to it:

<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class> <!-- path of "NoCacheFilter class" --<  </filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>

Leave a Reply

Your email address will not be published. Required fields are marked *