0

我有这个用于过滤缓存页面的代码,但在编译时遇到了一些问题:

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 Filterpublic void initpublic 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。如何实现这个过滤器的工作。

4

1 回答 1

1

Java EE 过滤器必须实现您上面提到的接口:javax.servlet.Filter。您的问题不在于Filter本身,而在于您的 Java 使用。在这种语言中,当某些非抽象类实现接口时,它或其父类必须实现所有声明的方法。这意味着,当接口声明方法时init(),即使实现应该为空,您的类也必须实现所有方法doFilter()destroy()这意味着您必须将两种解决方案结合起来:

  1. 取消注释implements Filter
  2. 让未注释的方法init()destroy()
  3. @Override如果您使用的是 Java 6 或更高版本,则可能取消注释

之后,您的过滤器应该没问题,至少它应该在 Web 容器处理web.xml映射中指定的某些页面匹配 URL 时执行。

无论如何,我想注销 URL 通常不会那么复杂,所以我希望映射 URL 类似于/logout. 正如我已经提到的,过滤器在与 URL 匹配的页面上执行。

于 2012-09-07T06:12:13.880 回答