-
什么是过滤器
当浏览器向服务器发送请求的时候,过滤器可以将请求拦截下来,完成一些特殊的功能,比如:编码设置、权限校验、日志记录等。
-
过滤器执行流程
过滤器的使用:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by.servlet;
import javax.servlet.*;
import java.io.IOException;
/**
* <p>Project: JavaWeb-Servlet - FilterDome</p>
* <p>Powered by scl On 2023-12-27 14:45:18</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class FilterDome implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");//设置请求的编码格式
servletResponse.setContentType("text/html;charset=utf-8");//设置相应的编码格式
System.out.println("---------come in----------");
filterChain.doFilter(servletRequest,servletResponse);//过滤器放行
System.out.println("------------good by------------");
}
@Override
public void destroy() {
}
}
测试代码:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* <p>Project: JavaWeb-Servlet - FileterTest</p>
* <p>Powered by scl On 2023-12-27 14:48:04</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class FilterTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.getWriter().write(username+password);
}
}
表单代码:
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2023/12/25
Time: 15:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="filter" method="get">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
配置文件web.xml:
<!--过滤器文件-->
<filter>
<filter-name>FilterDome</filter-name>
<filter-class>com.by.servlet.FilterDome</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDome</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--过滤器测试文件-->
<servlet>
<servlet-name>filterTest</servlet-name>
<servlet-class>com.by.servlet.FilterTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>filterTest</servlet-name>
<url-pattern>/filter</url-pattern>
</servlet-mapping>
-
使用过滤器需要注意的事项:
1.过滤器必须实现Filter接口。
2.过滤器拦截的请求执行完毕之后,必须要放行,否则我们的请求就不会被执行。
filterChain.doFilter(request,response); //过滤器放行
3.我们可以使用@WebFilter来配置过滤器要拦截的资源,当然我们也可以通过xml的方式配置过滤器。
<filter>
<filter-name>filter</filter-name>
<filter-class>com.by.servlet.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-
Filter的拦截路径的配置
1.拦截具体的资源路径:/index.jsp,只有访问index.jsp的时候才会被拦截
2.目录拦截:/user/*,访问/user下的所有资源,都会被拦截
3.后缀名拦截:*.jsp 访问后缀名为jsp的资源,都会被拦截
4.拦截所有:/* 访问所有的资源,都会被拦截