初级_过滤器的整理

来源:互联网 发布:macbookpro 软件 编辑:程序博客网 时间:2024/05/14 22:30

过滤器Filer属于Servlet的高级部分

            过滤器的位于jsp和servlet中间,会根据web.xml中配置的REQUEST(默认是请求),ERROR(错误)0或者INCLUDE(包含),FORWARD(转发)发生时,来访问我们的过滤器进行过虑

 生命周期           创建: 服务器启动的时候创建Filter的对象(init()).                   销毁:服务器关闭的时候或者是项目移除!(destory())

相关配置:    <filter>              

                        <filter-name>      :过滤器的名称

                         <filter-class>     :过滤器的全路径.

                                    <init-param>

                                                 <param-name>       :初始化参数的名称

                                                 <param-value>      :初始化参数的值

               <filter-mapping>

                        <filter-name>      :过滤器的名称

                        <url-pattern>      :过滤的路径.


过滤器的功能: 1.过滤敏感词汇   2.统一网站的字符集编码  3.进行相应压缩 4.进行url级别的权限控制

     案例      1:分IP统计网站访问次数

       代码体现

         过滤器部分

<span style="color:#000000;">public class IPFilter implements Filterprivate FilterConfig filterConfig;@Overridepublic void init(FilterConfig filterConfig) throws ServletException {// 创建一个Map集合:Map用于保存客户的IP和对应的访问次数:Map<String,Integer> map = new LinkedHashMap<String,Integer>();// 将Map集合存入到ServletContext域中.filterConfig.getServletContext().setAttribute("map", map);this.filterConfig = filterConfig;}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// 获得Map集合Map<String, Integer> map = (Map<String, Integer>) filterConfig.getServletContext().getAttribute("map");// 获得客户端IP地址:String ip = request.getRemoteAddr();// 判断IP是否已经在Map集合中.Integer count = map.get(ip);if(count == null){// 没有访问过这个网站.map.put(ip, 1);}else{// 之前访问过网站.count++;map.put(ip, count);}//放行:chain.doFilter(request, response);}@Overridepublic void destroy() {</span><span style="color:#000000;">}</span>
jsp部分
<body><h1>分IP统计网站的访问次数:</h1><table border="1" width="400"><tr><td>IP地址</td><td>访问次数</td></tr><c:forEach items="${ map }" var="entry"><tr><td>${ entry.key }</td><td>${ entry.value }</td></tr></c:forEach></table></body>

 

案例       2   网站通用的字符集编码过滤器:

代码体现

增强request中的getParameter方法: 用装饰者思想

public class MyReq extends HttpServletRequestWrapper{    private HttpServletRequest request;    public MyReq(HttpServletRequest request) {        super(request);        this.request=request;    }    @Override    public String getParameter(String name) {        // 判断请求的方式:        String type = request.getMethod();        if("get".equalsIgnoreCase(type)){            String value=null;            try {                value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");                value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"utf-8");            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }            return value;        }else if("post".equalsIgnoreCase(type)){            try {                request.setCharacterEncoding("UTF-8");            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }        return super.getParameter(name);    }

过滤器:

<strong><span style="font-size:14px;">public void init(FilterConfig filterConfig) throws ServletException {            }    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        HttpServletRequest reques=(HttpServletRequest)request;        MyReq myreq=new MyReq(reques);        chain.doFilter(myreq, response);    }    public void destroy() {            }</span></strong>

还不是很会排版.有点难..


1 0
原创粉丝点击