160909、Filter多方式拦截、禁用IE图片缓存、Filter设置字符编码

dispatcher多方式拦截 


我们来看一个例子

我们定义一个index.jsp,里面有一个链接跳转到dispatcher.jsp页面

<body>
    <a href="dispatcher.jsp">To Test Page</a>
</body>

我们来看下dispatcher.jsp页面,转发到test.jsp页面

<body>
    <jsp:forward page="/test.jsp"></jsp:forward>
</body>

 

我们来看下test.jsp页面,仅仅输入一行文字而已

<body>
    Test Page
</body>

当dispatcher.jsp页面,转发到test.jsp页面,我们是做2个拦截filter,我们看下web.xml中的配置吧

<filter>
        <filter-name>HelloFilter</filter-name>
        <filter-class>com.safly.HelloFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HelloFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>SecondFilter</filter-name>
        <filter-class>com.safly.SecondFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecondFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>
    </filter-mapping>

 

Filter过滤器对应的实例代码如下: 
HelloFilter

package com.safly;
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;
public class HelloFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("before helloFilter'chain");
        chain.doFilter(request, response);
        System.out.println("after hellofilter'chain");
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

SecondFilter

package com.safly;
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;
public class SecondFilter implements Filter {
    public void destroy() { 
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("before secondFilter'chain");
        chain.doFilter(request, response);
        System.out.println("after secondFilter'chain");
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

这里写图片描述 
我们点击上面的连接地址 
这里写图片描述


咦为啥?为什么没有起到拦截作用呢? 
这里写图片描述

原来默认的是Request方式,点击链接是转发的方式,那么我们怎么实现转发的方式,也通过过滤器拦截呢?我们只需要在web.xml文件中,进行修改,对SecondFilter和HelloFilter均做处理

<filter>
        <filter-name>HelloFilter</filter-name>
        <filter-class>com.safly.HelloFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HelloFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>SecondFilter</filter-name>
        <filter-class>com.safly.SecondFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecondFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

这里写图片描述

before helloFilter'chain
before secondFilter'chain
after secondFilter'chain
after hellofilter'chain

我们在进行修改,只对SecondFilter做处理

<filter>
        <filter-name>HelloFilter</filter-name>
        <filter-class>com.safly.HelloFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HelloFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>

    </filter-mapping>


    <filter>
        <filter-name>SecondFilter</filter-name>
        <filter-class>com.safly.SecondFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecondFilter</filter-name>
        <url-pattern>/test.jsp</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

 

这里写图片描述

before secondFilter'chain
after secondFilter'chain


解决ie浏览器图片缓存的问题 
ie模式是图片缓存的,那么我们怎么设置无缓存呢? 
这里写图片描述

我们定义一个b.jsp,里面有一个超链接,跳转到a.jsp

<body>  
    <a href="a.html">TO AAA PAGE</a>
</body>

 

我们再来看下a.jsp

<body>  
    <a href="b.html">TO BBB PAGE</a>
    <img alt="" src="Desert.jpg">
</body>

 

默认ie是自带缓存的,我们需要定义Filter过滤器进行设置,那我们先看下web.xml中的过滤器设置吧

  <filter>
    <display-name>NoCacheFilter</display-name>
    <filter-name>NoCacheFilter</filter-name>
    <filter-class>com.safly.NoCacheFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>NoCacheFilter</filter-name>
    <url-pattern>/cache/*</url-pattern>
  </filter-mapping>

 

/cache/*代表对cache目录下的所有页面进行设置操作

HttpFilter

package com.safly;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 自定义的 HttpFilter, 实现自 Filter 接口
 */
public abstract class HttpFilter implements Filter {
    /**
     * 用于保存 FilterConfig 对象. 
     */
    private FilterConfig filterConfig;
    /**
     * 不建议子类直接覆盖. 若直接覆盖, 将可能会导致 filterConfig 成员变量初始化失败
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        init();
    }
    /**
     * 供子类继承的初始化方法. 可以通过 getFilterConfig() 获取 FilterConfig 对象. 
     */
    protected void init() {}
    /**
     * 直接返回 init(ServletConfig) 的 FilterConfig 对象
     */
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }
    /**
     * 原生的 doFilter 方法, 在方法内部把 ServletRequest 和 ServletResponse 
     * 转为了 HttpServletRequest 和 HttpServletResponse, 并调用了 
     * doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
     * 
     * 若编写 Filter 的过滤方法不建议直接继承该方法. 而建议继承
     * doFilter(HttpServletRequest request, HttpServletResponse response, 
     *      FilterChain filterChain) 方法
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        doFilter(request, response, chain);
    }

    public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, 
            FilterChain filterChain) throws IOException, ServletException;
    @Override
    public void destroy() {}
}

 

NoCacheFilter

package com.safly;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NoCacheFilter extends HttpFilter {
    @Override
    public void doFilter(HttpServletRequest request,
            HttpServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("cacheFilter's doFilter..");

        response.setDateHeader("Expires",-1);
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","no-cache");    
        filterChain.doFilter(request, response);
    }
}

 

这样就实现了ie浏览器的非缓存设置


过滤器-字符编码

这里写图片描述

a.jsp输入name提交到b.jsp

<body>
    <form action="b.jsp" method="post">
        name: <input type="text" name="name"/>
        <input type="submit" value="Submit"/>
    </form>
</body>

b.jsp显示输入的用户名

<body>
    Hello: ${param.name }
</body>

如果输入中文字符可能出现乱码,如下的情况 
这里写图片描述

我们需要进行过滤器设置

    <context-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </context-param>
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.safly.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/encoding/*</url-pattern>
    </filter-mapping>

EncodingFilter

package com.safly;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class EncodingFilter extends HttpFilter{

    private String encoding;

    @Override
    protected void init() {
        encoding = getFilterConfig().getServletContext().getInitParameter("encoding");
    }

    @Override
    public void doFilter(HttpServletRequest request,
            HttpServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println(encoding); 
        request.setCharacterEncoding(encoding);
        filterChain.doFilter(request, response);
    }
}

依然用上例进制浏览器图片缓存的HttpFilter基类 
这里写图片描述 
浏览器输出 
这里写图片描述

posted @ 2016-09-10 16:30  目标奔雷手  阅读(292)  评论(0编辑  收藏  举报