zoukankan      html  css  js  c++  java
  • Servlet3.0-使用注解定义过滤器(Filter)

    本人正在学javaweb,遇到了在eclipse中,servlet3.0过滤器需不需要配置web.xml文件?通过实践得出结论,不用配置,只需要@WebFilter(filterName="过滤器名称",urlPatterns="/*")这句话就行了。答案是从一下文章中找到的。

    Servlet3.0提供@WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器组件。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package com.cndatacom.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.annotation.WebFilter;
     
     
    /**
     * 使用注解定义编码过滤器
     * @author Luxh
     */
     
     
    /**
     * @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器组件
     * 属性filterName声明过滤器的名称,可选
     * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
     */
    @WebFilter(
    filterName = "shiroFilter",
    urlPatterns = "/*",
    initParams = {
    @WebInitParam(name = "targetFilterLifecycle", value = "true")
    }
    )
    public class ShiroFilterimplements Filter {
         
        @Override
        public void init(FilterConfig arg0) throws ServletException {
     
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain filterChain) throws IOException, ServletException {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            filterChain.doFilter(request, response);
        }
     
        @Override
        public void destroy() {
             
        }
     
    }

      @WebFilter(filterName="shiroFilter",urlPatterns="/*")

      @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器组件

      属性urlPatterns指定要过滤的URL模式,也可以用属性value来指定。

      可以指定多种过滤模式@WebFilter(filterName="encodingFilter",urlPatterns={"/UserManagerServlet","/index.jsp"})

      web.xml依然是无需任何配置的。

    [html] view plain copy
     
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="3.0"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    7.     
    8. <!-- shiroFilter : DelegatingFilterProxy作用是自动到spring容器查找名字为shiroFilter(filter-name)的bean并把所有Filter的操作委托给它。然后将shiroFilter配置到spring容器即可 -->
      <filter>
      <filter-name>shiroFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
      </init-param>
      </filter>
      <filter-mapping>
      <filter-name>shiroFilter</filter-name>
      <url-pattern>/*</url-pattern>
      </filter-mapping>
    9. </web-app>  

      Servlet3.0规范为开发人员实现了:

      简单性

      减轻开发工作量

      遵循web 2.0原则

    转自:http://www.cnblogs.com/luxh/archive/2012/06/06/2538903.html

  • 相关阅读:
    第六章 函数、谓词、CASE表达式 6-3 CASE表达式
    第六章 函数、谓词、CASE表达式 6-2 谓词
    第六章 函数、谓词、CASE表达式 6-1 各种各样的函数
    第五章 复杂查询 5-3 关联子查询
    第五章 复杂查询 5-2 子查询
    第五章 复杂查询 5-1 视图
    第四章 数据更新 4-3 事务
    第四章 数据库和SQL 4-3 数据的更新(UPDATE语句的使用方法)
    面向对象进阶
    多态
  • 原文地址:https://www.cnblogs.com/xiaoxiaoccaiya/p/7641249.html
Copyright © 2011-2022 走看看