1. 程式人生 > >Java filter中的chain.doFilter詳解

Java filter中的chain.doFilter詳解

except detail jsp app pub 正常的 如果 舉例 splay

轉載:

一.chain.doFilter作用

1.一般filter都是一個鏈,web.xml 裏面配置了幾個就有幾個。一個一個的連在一起
request -> filter1 -> filter2 ->filter3 -> …. -> request resource.

2.chain.doFilter將請求轉發給過濾器鏈下一個filter , 如果沒有filter那就是你請求的資源

二.舉例

  1. input.jsp 是用來提交輸入的 : 當提交後,過濾器檢測姓名和年齡 .
  2. 如果正常的話會提交給output.jsp 如果不正常提交給erroroutput.jsp.
  3. 在此 , 同時也有一個過濾器,防止亂碼問題存在 , 該過濾器檢測一個頁面是否設置了字符編碼,如果沒有則進行設置。

1.input.jsp


<form action="output.jsp" name="form" method="post">
    <table>
        <tr>
            <td>name</td>
            <td><input type="text" name="name" /></td>
        </tr>
        <tr>
            <td>age</td>
            <td><input type="
text" name="age"/></td> </tr> <tr> <td><input type="submit" name="ok" value="ok"/></td> </tr> </table> </form>

2.web.xml

<filter>
    <description></description>
    <display-name>encodefilter</display-name>
    <filter-name>encodefilter</filter-name>
    <filter-class
>servletbean.encodefilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodefilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <description></description> <display-name>myfilter</display-name> <filter-name>myfilter</filter-name> <filter-class>servletbean.myfilter</filter-class> </filter> <filter-mapping> <filter-name>myfilter</filter-name> <url-pattern>/output.jsp</url-pattern> </filter-mapping>

3.encodefilter.java

package servletbean;
public class encodefilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if(request.getCharacterEncoding()==null) {
            System.out.println(encoding);
            request.setCharacterEncoding(encoding);
        }
        chain.doFilter(request, response);//到下一個鏈
    }
    public void init(FilterConfig fConfig) throws ServletException {
        this.config=fConfig;
        encoding=fConfig.getInitParameter("encoding");//獲得目標編碼格式
    }
}

3.myfilter.java

package servletbean;
import javax.swing.JOptionPane;

public class myfilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html");
        response.setCharacterEncoding("GB2312");

        PrintWriter out=response.getWriter();
        String name="";
        String age="";
        int age1;

        name=request.getParameter("name");
        age=request.getParameter("age");
        RequestDispatcher dispatch=request.getRequestDispatcher("erroroutput.jsp");
        if(name==null||name==""||name==" "||age==null) {
            JOptionPane.showMessageDialog(null,"用戶名和年齡輸入錯誤!");
            dispatch.forward(request, response);
            return;
        }
        else{
            try {
                age1=Integer.parseInt(age);
            }catch(Exception e){
                //JOptionPane.showMessageDialog(null,"年齡必須為數字!");
                dispatch.forward(request,response);
                return;//如果是錯誤頁面就到erroroutput.jsp中
            }
        }
        //這裏表示是正確的,也就是說,他回去找下一個鏈,但是它下面已經沒有了,所以就會去跳轉頁面了,此跳轉的頁面就是action="output.jsp"了
        chain.doFilter(request, response);
        }
}

轉載地址:http://blog.csdn.net/haofengjiao/article/details/70186381

Java filter中的chain.doFilter詳解