如何解决用户LOGOUT的FILTER查询问题

时间:2012-09-07 05:45:03

标签: java jsp servlet-filters

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

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

所以我在下面的代码中进行了更改然后编译但是我没有得到理想的结果。我做的更改是: 我在评论中添加了@Overrideimplements Filter,并添加了public 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>

我希望此过滤器限制用户在LOGOUT之后返回到我apps目录中的先前缓存页面,因此我使用url-pattern。 如何实现这个过滤器。

1 个答案:

答案 0 :(得分:1)

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

  1. 取消注释implements Filter
  2. 取消注释方法init()destroy()
  3. 如果您使用的是Java 6或更高版本,
  4. 可能会取消注释@Override
  5. 之后您的过滤器应该没问题,至少应该在Web容器处理web.xml映射中指定的页面匹配URL时执行。

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