目的是完成未正规登陆的过滤问题
过滤类是这么写的public class OnlineFilter  implements javax.servlet.Filter {   
    
 String loginPage="index.jsp";    //没有登录的话,你要跳转的目录
 protected FilterConfig filterConfig;
 
 public void destroy() {
   filterConfig=null;
 }  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   HttpServletRequest req = (HttpServletRequest) request;
   HttpServletResponse res = (HttpServletResponse) response;
   HttpSession session = req.getSession();
   String isLogin = (String)session.getAttribute("login");
   System.out.println(isLogin);   //测试
   if (isLogin!=null && isLogin.equals("yes")){    //判断session值是否正确
     chain.doFilter(request,response);
   }else{
     res.sendRedirect(loginPage);        //失败的话,sendRedirect到指定页面
   }
 }  public void init(FilterConfig filterConfig) throws ServletException {
   this.filterConfig=filterConfig;
 }
}
web.xml这样配置的(片段)<filter>
    <filter-name>IsLogin</filter-name>
    <filter-class>filter.OnlineFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>IsLogin</filter-name>
    <url-pattern>/client/*</url-pattern>
</filter-mapping>
意思想过滤client目录下所有文件
index.jsp是在webroot目录下的如下结构 
webroot
 | |  
 | |
 | client
 | |
 | |-----------XXX.jsp
 |
 index.jsp
    若我尝试直接用http:localhose:8888/myproject/client/xxx.jsp 类型登陆时 就一直卡住页面 看控制台打印 null  一直在打印null 死循环

解决方案 »

  1.   

    loginPage 是不是应该写成"../login.jsp"呀,呵呵
      

  2.   

    if (isLogin!=null && isLogin.equals("yes")){    //判断session值是否正确
             chain.doFilter(request,response);
             return
           }else{
             res.sendRedirect(loginPage);        //失败的话,sendRedirect到指定页面
           }
      

  3.   

    谢谢楼上,要是我还要匹配几个文件夹怎么写了?
    比如在webroot下的另外个文件夹名字为 port
    如果把这个也加上去了?
      

  4.   

    你那是叫什么过滤器?
    根本就没有达到你要的结果
    根本看不到关于你是怎么过滤client目录下所有文件
      

  5.   

    如果是用servlet,那就直接可以用doPost 或doGet
      

  6.   

    主要问题在于loginPage应该使用client/*以外的地址。
    比如我们教程中的例子
    loginPage就在/login.jsp上。而受保护的对象在/admin/*,这样就不会出现死循环。我们教程中的例子:
    http://www.family168.com/tutorial/jsp/html/jsp-ch-07.htm#jsp-ch-07-02或者考虑使用forward
      

  7.   

    直接在映射中配置<filter-mapping>
        <filter-name>IsLogin</filter-name>
        <url-pattern>/port/*</url-pattern>
    </filter-mapping>
      

  8.   

    谢谢大家了,还是那个sendredirect 路径错误了
    我直接用最SB的办法
    改成这样
     String path = ((HttpServletRequest) request).getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";

       if (isLogin!=null && isLogin.equals("yes")){    //判断session值是否正确
         chain.doFilter(request,response);
         return;
       }else{
         res.sendRedirect(basePath+"index.jsp");        //失败的话,sendRedirect到指定页面
       }
     }
      

  9.   

    如果你的请求地址是http://localhost:8888/myproject/client/xxx.jsp,当请求被过滤器拦截并进行sendRedirect("index.jsp");,服务器会产生302响应信息连同重定向到的URL一同发给浏览器,如果sendRedirect参数是相对URL,即不是以/开头,服务器会使用请求地址来处理重定向URL,即将处理后的URL/myproject/client/index.jsp发给浏览器,之后浏览器的请求实际上被重定向到了client路径下,再次被过滤器拦截,再次重复上面的过程,结果可想而知。
      

  10.   

    应该将String loginPage="index.jsp";改为String loginPage="../index.jsp";或者String loginPage="/myproject/index.jsp";