当前位置: 代码迷 >> Web前端 >> Tomcat与web开发技术详解读书笔记(二)过滤器
  详细解决方案

Tomcat与web开发技术详解读书笔记(二)过滤器

热度:400   发布时间:2012-11-22 00:16:41.0
Tomcat与web开发技术详解读书笔记(2)过滤器

1.简介

  • 过滤器能够在ServletJSPHTMLweb组件被调用之前检查ServletRequest对象,修改请求头和请求正文的内容或者对请求进行预处理操作
  • 能够在ServletJSPHTMLweb组件被调用之后检查ServletReponse对象,修改响应头和响应正文。
  • 利用包装类ServletRequestWrapperServletResponseWrapper来修改ServletRequest对象和ServletResponse对象。

2.如何创建过滤器?

必须实现javax.servlet.Filter接口。

  • init(FilterConfig config):过滤器初始化方法。Web应用启动时,Servlet容器首先创建FilterConfig对象(包含过滤器配置信息),然后创建Filter对象,接着调用Filter对象的init(FilterConfig config)方法,读取web.xml中的配置参数。
  •  public class LoginFilter implements Filter{
        private FilterConfig config;
        
        private String username;
    
        @Override
        public void destroy() {
            this.config = null;
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
            //HttpServletRequest httpRequest = (HttpServletRequest)request;
            HttpServletResponse httpResponse = (HttpServletResponse)response;
            System.out.println("登录过滤器验证");
            if(StringUtils.equalsIgnoreCase(username, "flyer")){
                System.out.println("验证通过");
                chain.doFilter(request, response);
            }else {
                System.out.println("验证被拦截");
                httpResponse.sendRedirect("login.jsp");
            }
        }
    
        @Override
        public void init(FilterConfig config) throws ServletException {
            this.config = config;
            username = config.getInitParameter("username");
        }
    
    }
    ??
    web.xml中添加过滤器配置

    ?

    ?

    Xml代码?
    1. <filter>??
    2. ????????<filter-name>loginFilter</filter-name>??
    3. ????????<filter-class>com.flyer.filter.LoginFilter</filter-class>??
    4. ????????<init-param>??
    5. ????????????<param-name>username</param-name>??
    6. ????????????<param-value>flyer</param-value>??
    7. ????????</init-param>??
    8. ????</filter>??
    9. ????<filter-mapping>??
    10. ????????<filter-name>loginFilter</filter-name>??
    11. ????????<url-pattern>??
    12. ????????????/*??
    13. ????????</url-pattern>??
    14. ????</filter-mapping>??
    ?web开发中常用的字符集编码过滤器

    ?

    ?

    Xml代码?
    1. <filter>??
    2. ????????<filter-name>encodingFilter</filter-name>??
    3. ????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>??
    4. ????????<init-param>??
    5. ????????????<param-name>encoding</param-name>??
    6. ????????????<param-value>??
    7. ????????????????UTF-8??
    8. ????????????</param-value>??
    9. ????????</init-param>??
    10. ????</filter>??
    11. ????<!--?过滤器能为所有的url过滤?-->??
    12. ????<filter-mapping>??
    13. ????????<filter-name>encodingFilter</filter-name>??
    14. ????????<url-pattern>??
    15. ????????????/*??
    16. ????????</url-pattern>??
    17. ????</filter-mapping>??
    ?补充:Servlet监听器 ??类似与Swing界面应用开发,Servlet也可以创建监听器,以对Servlet容器,或Servlet中以象的事件做出反应。Servlet监听器主要有以下几种: ServletRequestListener ,ServletRequestAttributeListener, HttpSessionActivationListener ,HttpSessionBindingListener , HttpSessionAttributeListener,HttpSessionListener, ServletContextListener等等。 ??这些监听器主要用来监听session,request,application这三个对象里存取数据的变化。?


?

  相关解决方案