zoukankan      html  css  js  c++  java
  • 【Head First Servlets and JSP】笔记 28: 过滤器与包装器

    1、过滤器的执行顺序: <url-pattern> 为第一梯队, <servlet-name> 为第二梯队,梯队内的执行顺序和 DD 里的声明顺序相同。

    When the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain.Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain.In both the sets the filters are executed in the order in which they are declared in the D.D.

    2、可以对服务器中传递的请求进行过滤。

        <filter>
            <filter-name>BeerRequest</filter-name>
            <filter-class>sample.BeerRequestFilter</filter-class>
            <init-param>
                <param-name>LogFileName</param-name>
                <param-value>Userlog.txt</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>BeerRequest</filter-name>
            <url-pattern>*.do</url-pattern>
            <servlet-name>jack</servlet-name>
            <dispatcher>REQUEST</dispatcher> <!-- 当一个也没有设置时,它是默认值 -->
            <dispatcher>INCLUDE</dispatcher>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>

    3、过滤器应用实例

    package sample;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Servlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter printWriter = resp.getWriter();
            printWriter.println("calls doGet(req, resp)");
            printWriter.println("测试能否输出中文");
            printWriter.close();
        }
    }

    filter :

    package sample;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class Filter implements javax.servlet.Filter{
    
        private int visited;
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletResponse.setContentType("text/html");
            servletResponse.setCharacterEncoding("utf-8");
            visited ++;
            filterChain.doFilter(servletRequest, servletResponse);
            System.out.println("visited = " + visited);
        }
    
        @Override
        public void destroy() {
    
        }
    }

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <servlet>
            <servlet-name>servlet</servlet-name>
            <servlet-class>sample.Servlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>servlet</servlet-name>
            <url-pattern>/do</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>filter</filter-name>
            <filter-class>sample.Filter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>filter</filter-name>
            <servlet-name>servlet</servlet-name>
        </filter-mapping>
    
    </web-app>

    4、包装器略。


  • 相关阅读:
    剑指OFFER 连续数组的最大和
    剑指OFFER 两个链表的第一个公共结点
    剑指OFFER 替换空格
    剑指OFFER 二叉树中和为某一值的路径
    剑指OFFER 二叉树的镜像
    剑指OFFER 从上往下打印二叉树
    剑指OFFER 删除链表中重复的结点
    剑指OFFER 数组中只出现一次的数字
    剑指OFFER 调整数组顺序使奇数位于偶数前面
    C 语言 sleep 函数
  • 原文地址:https://www.cnblogs.com/xkxf/p/7323908.html
Copyright © 2011-2022 走看看