大数据必学Java基础(一百零七):过滤器配置

2022-12-17 11:04:42 浏览数 (1)

过滤器配置

采用三步走策略使用filter

  1. 开发后台资源 静态资源(html,css ……)或者动态资源(Servlet,Jsp)
  2. 开发Filter
  3. 在web.xml中配置Filter拦截哪些资源

一、开发Servlet

定义两个servlet

代码语言:javascript复制
package com.lanson.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
public class MyServlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("myServlet1 执行了sevice方法");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("myServlet1响应的数据");
    }
}

代码语言:javascript复制
package com.lanson.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
public class MyServlet2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("myServlet2 执行了sevice方法");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("myServlet2响应的数据");
    }
}

二、开发Filter

代码语言:javascript复制
package com.lanson.filter;
import javax.servlet.*;
import java.io.IOException;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
public class MyFilter implements Filter {
    // 初始化方法
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    // 作出过滤的方法
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter doFilter 对请求作出过滤");
        // 通过一行代码 放行请求
        // 放行请求,交给过滤器链继续进行过滤 最后到达资源
        filterChain.doFilter(servletRequest, servletResponse);
        
        System.out.println("Filter doFilter 对响应作出过滤");
        
        servletResponse.getWriter().print("filter 追加一些数据");
    }
    // 销毁方法
    @Override
    public void destroy() {
    }
}

三、配置Filter

代码语言:javascript复制
<?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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>myServlet1</servlet-name>
        <servlet-class>com.lanson.servlet.MyServlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet1</servlet-name>
        <url-pattern>/myServlet1.do</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>myServlet2</servlet-name>
        <servlet-class>com.lanson.servlet.MyServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet2</servlet-name>
        <url-pattern>/myServlet2.do</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>filter1</filter-name>
        <filter-class>com.lanson.filter.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filter1</filter-name>
        <!--对那个/些资源的请求和响应进行过滤-->
        <!--<url-pattern>/myServlet1.do</url-pattern>-->
        <servlet-name>myServlet1</servlet-name>
        <servlet-name>myServlet2</servlet-name>
        <!--<url-pattern>/</url-pattern>
        <url-pattern>/*</url-pattern>-->
    </filter-mapping>
</web-app>

0 人点赞