首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

验证登录过滤器类怎么配置web.xml

2012-06-11 
验证登录过滤器类如何配置web.xml从网上下了个验证登录的过滤器类,不知道如何配置web.xml中的参数代码如下

验证登录过滤器类如何配置web.xml
从网上下了个验证登录的过滤器类,不知道如何配置web.xml中的参数
代码如下:

Java code
/** * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面  * 配置参数  * checkSessionKey 需检查的在 Session 中保存的关键字 * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath  * notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath */public class LoginFilter implements Filter {    protected FilterConfig filterConfig = null;    private String redirectURL = null;    private List notCheckURLList = new ArrayList();    private String sessionKey = null;    public void init(FilterConfig filterConfig) throws ServletException {        this.filterConfig = filterConfig;        redirectURL = filterConfig.getInitParameter("redirectURL");        sessionKey = filterConfig.getInitParameter("checkSessionKey");        String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");        if (notCheckURLListStr != null) {            StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");            notCheckURLList.clear();            while (st.hasMoreTokens()) {                notCheckURLList.add(st.nextToken());            }        }    }    public void doFilter(ServletRequest servletRequest,            ServletResponse servletResponse, FilterChain filterChain)            throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) servletRequest;        HttpServletResponse response = (HttpServletResponse) servletResponse;        HttpSession session = request.getSession();        if (sessionKey == null) {            filterChain.doFilter(request, response);            return;        }        if ((!checkRequestURIIntNotFilterList(request))                && session.getAttribute(sessionKey) == null) {            response.sendRedirect(request.getContextPath() + redirectURL);            return;        }        filterChain.doFilter(servletRequest, servletResponse);    }    public void destroy() {        notCheckURLList.clear();    }    private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {        String uri = request.getServletPath()                + (request.getPathInfo() == null ? "" : request.getPathInfo());        return notCheckURLList.contains(uri);    }}


web.xml文件:
  <filter>
  <filter-name>checkLogin</filter-name>
  <filter-class>com.broadengate.zensar.coepms.application.common.filter.LoginFilter</filter-class>
  <init-param>
  <param-name>redirectURL</param-name>
  <param-value>/index.jsp</param-value>
  </init-param>
  <init-param>
  <param-name>checkSessionKey</param-name>
  <param-value>???</param-value>
  </init-param>
  <init-param>
  <param-name>notCheckURLList</param-name>
  <param-value>lo???????param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>checkLogin</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

不知道???那怎么写,还有页面怎么写session



[解决办法]
比如我的过滤器配置是:
用来过滤编码的
XML code
    <filter>        <filter-name>Set Character Encoding</filter-name>        <filter-class>com.yj.util.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>GBK</param-value>        </init-param>    </filter> 


[解决办法]
checkSessionKey的参数,咱们可以这样来知道到底如何配置:^^
首先看下源码。在filter init的时候

Java code
sessionKey = filterConfig.getInitParameter("checkSessionKey"); 

热点排行