我有这个用于过滤缓存页面的代码,但在编译时遇到了一些问题:
package bean.log.filter;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.annotation.WebFilter;
public class LoginFilter implements Filter
{
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
try
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("userHash") == null)
{
response.sendRedirect("/scape/applicationservices/fileshare/vm/login/login.jsp"); // No logged-in user found, so redirect to login page.
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);
}
else
{
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
编译后我得到以下错误:
D:\programs\MyPackage\bean\log\filter>javac LoginFilter.java
LoginFilter.java:14: bean.log.filter.LoginFilter is not abstract and does not ov
erride abstract method destroy() in javax.servlet.Filter
public class LoginFilter implements Filter
^
1 error
所以我对我的代码进行了如下更改,然后编译了它,但我没有得到想要的结果。我所做的更改是:我在评论中添加和添加@Override
了和方法implements Filter
public void init
public void destroy
package bean.log.filter;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.annotation.WebFilter;
public class LoginFilter // implements Filter
{
//@Override
public void init( )
{
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
try
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("userHash") == null)
{
response.sendRedirect("/timescape/applicationservices/fileshare/vm/login/login.jsp"); // No logged-in user found, so redirect to login page.
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);
}
else
{
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void destroy( )
{
}
}
如何在 web.xml 中配置过滤器
我像下面这样配置它
<web-app>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>noCacheFilter</filter-name>
<filter-class>bean.log.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>noCacheFilter</filter-name>
<url-pattern>/scape/applicationservices/fileshare/vm/apps/*</url-pattern>
</filter-mapping>
</web-app>
我希望这个过滤器限制用户在注销后返回到我目录中的以前的缓存页面,apps
所以我使用它url-pattern
。如何实现这个过滤器的工作。