• Filter


    Filter:过滤器,用来过滤器网站的数据

    作用:处理中文乱码

       登录验证

        

    Filter开发步骤

      1.导包

        <groupId>com.king</groupId>
        <artifactId>javaweb-filter</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.3</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!--standard标签库-->
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
    
            <!--连接数据-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
            </dependency>
    
        </dependencies>

      2.编写过滤器

    package com.king.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class CharacterEncodingFilter implements Filter {
    
       //初始化,web服务器启动,就已经初始化了,随时等待过滤对象启动
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("CharacterEncodingFilter初始化");
        }
    
        //chain 链
        /*
        * 过滤中的所有代码,在过滤特定请求的时候都会执行
        * 必须要让过滤器继续执行,chain.doFilter(request,response);
        * */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
    
            System.out.println("filter过滤器执行前");
            chain.doFilter(request,response);//让请求继续走,如果不写,程序到这里就被拦截停止
            System.out.println("filter过滤器执行后");
        }
    
        //销毁,web服务器关闭时候,过滤器销毁
        public void destroy() {
            System.out.println("CharacterEncodingFilter销毁");
    
        }
    }

    3.测试类

    package com.king.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class ShowServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //resp.setCharacterEncoding("utf-8");
            resp.getWriter().write("你好,java");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

    4.web配置

      <servlet>
            <servlet-name>ShowServlet</servlet-name>
            <servlet-class>com.king.servlet.ShowServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ShowServlet</servlet-name>
            <url-pattern>/servlet/show</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>ShowServlet</servlet-name>
            <url-pattern>/show</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.king.filter.CharacterEncodingFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <!--只要是/servlet的任何请求,都会经过这个过滤器-->
            <url-pattern>/servlet/*</url-pattern>
        </filter-mapping>

    总结:在设置完过滤器具体功能后,要chain.daofilter之后过滤器才起作用,

       具体原理课百度搜chain链处理模式

  • 相关阅读:
    【Vue前端】Vue前端注册业务实现!!!【代码】
    QQ第三方登录逻辑(微信,微博等同)
    发送短信验证码逻辑
    web图形验证码逻辑
    PID算法资料【视频+PDF介绍】
    如何配置电脑本地的域名
    js实现阻止默认事件preventDefault与returnValue
    js实现事件监听与阻止监听传播
    json字符串转换对象的方法1
    json字符串转换对象的方法
  • 原文地址:https://www.cnblogs.com/CL-King/p/13782726.html
Copyright © 2020-2023  润新知