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、包装器略。


  • 相关阅读:
    解决软件卸载时Abstract: "Invalid serial number" xe4
    Delphi 数据类型列表
    delphi self.Update 什么作用
    delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)
    H5单页面架构:自定义路由 + requirejs + zepto + underscore
    H5单页面架构:backbone + requirejs + zepto + underscore
    H5单页面架构:requirejs + angular + angular-route
    基于angularJS和requireJS的前端架构
    单页面应用SPA架构
    自动化前端项目构建
  • 原文地址:https://www.cnblogs.com/xkxf/p/7323908.html
Copyright © 2011-2022 走看看