`
黑暗天使
  • 浏览: 96425 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

在过滤器中对ignoreURL进行处理

    博客分类:
  • java
阅读更多

      公司的框架提供了对页面表格的验证功能,但必须要通过webfaster(类似于action)转到页面才会解析,因此在利用过滤器判断session是否失效时一定要放掉login.webfaster,于是写了一个过滤器来实现对ignoreURL的处理

过滤器代码:

 

import java.util.ArrayList;
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.HttpSession;
import java.io.IOException;

public class FilterTest implements Filter{
    protected String ignoreURL = null;
    protected String [] allIgnoreURL = null; 
    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();
        String url=req.getRequestURL().toString();
        url= url.substring(url.lastIndexOf("/"));//获取*.webfaster
        if(isIgnoreURL(url)){//如果是过滤器要忽略的url则继续执行请求
            chain.doFilter(request, response);
        }else if(session.getAttribute("userinfo")==null){//若session失效则通过relogin.webfaster转到login.html页面
            StringBuffer scriptString=new StringBuffer(); 
            scriptString.append(" <script>\n\r"); 
            scriptString.append("self.top.location.href=\""+"relogin.webfaster"+"\"\n\r");
            scriptString.append(" </script>\n\r"); 
            response.getOutputStream().print(scriptString.toString());  
        }else{
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig config) throws ServletException {
        this.ignoreURL=config.getInitParameter("ignoreURL");
        allIgnoreURL=ignoreURL.split(",");   
    }
    
    public boolean isIgnoreURL(String url)
    {
      for (int i = 0; i < this.allIgnoreURL.length; i++)
      {
        if (url.equals(this.allIgnoreURL[i])) {
          return true;
        }
      }
      return false;
    }

}

 WEB.XML中的配置:

 <filter>       
      <filter-name>FilterTest</filter-name>       
      <filter-class>com.servyou.filter.FilterTest</filter-class> 
      <init-param>
            <param-name>ignoreURL</param-name>
            <param-value>
            /relogin.webfaster,/login.webfaster
            </param-value>
        </init-param>    
   </filter>   
   <filter-mapping>       
    <filter-name>FilterTest</filter-name>       
    <url-pattern>*.webfaster</url-pattern>    
  </filter-mapping> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics