当前位置:网站首页>Filter filter
Filter filter
2022-07-24 07:14:00 【SJMP1974】
Reference resources :https://www.bilibili.com/video/BV1Y7411K7zz?p=293&vd_source=09e3f81b9f7ed8ee4306bb2e0ecd5538
One 、 What is? Filter filter ?
1、Filter Filter it's JavaWeb One of the three components of , Namely :Servlet Program 、Listener Monitor 、Filter filter .
2、Filter Filter it's JavaEE The specification of , That's the interface .
3、Filter The function of the filter is : Intercept request , Filtering response .
Common application scenarios for intercepting requests are :
1、 Permission check
2、 Diary operation
3、 Business management
……
Two 、 How to use Filter
2.1 Write a Filter class , And identify @WebFilter annotation
package com.atguigu.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/** * @author: sjmp1573 * @date: 2022/7/23 21:23 * @description: */
@WebFilter
public class AdminFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpSession session = httpServletRequest.getSession();
if (session.getAttribute("user") == null) {
request.getRequestDispatcher("/login.html").forward(request, response);
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
2.2 stay web-xml Middle configuration
<?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">
<filter>
<filter-name>AdminFilter</filter-name>
<filter-class>com.atguigu.filter.AdminFilter</filter-class>
<init-param>
<param-name>book1</param-name>
<param-value>book1</param-value>
</init-param>
<init-param>
<param-name>book2</param-name>
<param-value>book2</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AdminFilter</filter-name>
<url-pattern>/admin/*</url-pattern> </filter-mapping> </web-app> 2.3 Writing a login permission check is Servlet
package com.atguigu.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/** * @author: sjmp1573 * @date: 2022/7/23 22:30 * @description: */
@WebServlet(name = "adminServlet", value = "/adminServlet")
public class AdminServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
HttpSession session = req.getSession();
if (username.equals("admin") && password.equals("admin")) {
if (session.getAttribute("user") == null) {
// Request parameters == null , Description is the first login
session.setAttribute("user",username);
}
resp.getWriter().write("ok!!!");
} else {
req.getRequestDispatcher("login.html").forward(req, resp);
}
}
}
3、 ... and 、Filter Life cycle of
Filter The life cycle of is composed of several methods
1、 Constructor method
2、init Initialization method
The first 1,2 Step , stay web When the project starts (Filter Created )
3、doFilter Filtration method
The first 3 Step , Every time a request is intercepted , Will execute
4、destroy The destruction
The first 4 Step , stop it web During the project , Will execute ( stop it web engineering , It will be destroyed Filter filter )
Four 、FilterConfig class
FilterConfig The name and meaning can be seen by category , It is Filter Filter profile class .
Tomcat Each creation Filter When , It will also create a FilterConfig class , It contains Filter Configuration information of the configuration file .
FilterConfig The role of a class is to get filter Filter configuration content
1、 obtain Filter The name of filter-name The content of
2、 To get in Filter Configured in init-param Initialize parameters
3、 obtain ServletContext object
5、 ... and 、FilterChain Filter chain

6、 ... and 、Filter The interception path of
Exactly match
<url-pattern>/target.jsp</url-pattern>
The path of the above configuration , Indicates that the request address must be :http://ip:port/ Project path /target.jspDirectory matching
<url-pattern>/admin/*</url-pattern>
The path of the above configuration , Indicates that the request address must be :http://ip:port/ Project path /admin/*Suffix matches
<url-pattern>*.html</url-pattern>
The path of the above configuration , Indicates that the request address must be in .html The end will intercept
Filter The filter only cares about whether the requested address matches , Does not care if the requested resource exists !!!
边栏推荐
- RIoTBoard开发板系列笔记(九)—— buildroot 移植MatchBox
- OWASP TOP10 penetration test
- Flow control statement of avascript
- 17. What is the situation of using ArrayList or LinkedList?
- Basic syntax of MySQL DDL and DML and DQL
- STM32 ADC based on Hal library uses DMA multi-channel sampling and solves the problems encountered
- Empty cup mentality, start again
- Traditional e-commerce dividends disappear, how to enter the new social e-commerce?
- After grouping, return to the last record group in each group_ Use of concat
- Libevent and multithreading
猜你喜欢

聚合型新生态模式-分享购,会员及奖励制度

变量和数据类型(04)完结

第一部分—C语言基础篇_11. 综合项目-贪吃蛇

C language from entry to soil (I)

Hackingtool of security tools

【方向盘】IDEA的代码审查能力,来保证代码质量

In the era of e-commerce, what should enterprises do in the transformation of social e-commerce?

Aggregated new ecological model - sharing purchase, membership and reward system

Can recursion still play like this? Recursive implementation of minesweeping game

STM32 ADC based on Hal library uses DMA multi-channel sampling and solves the problems encountered
随机推荐
Love yourself first, then others.
一个怎样的模式能让平台用户发生自助裂变?-链动2+1
STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
5. Template cache. Drawing a square can only move within the triangle
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
Prompt for garbled code when CHM file is opened
单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
GIMP自定义截图
Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx
yocs_ velocity_ Smooth source code compilation
QoS服务质量三DiffServ模型报文的标记及PHB
STM32 external interrupt (register version)
C language from entry to soil (I)
Traditional e-commerce dividends disappear, how to enter the new social e-commerce?
[C language] operator details (in-depth understanding + sorting and classification)
上传图片base64
Design a function print to print the string. If only the string parameter s is passed, the string length is compared with 10, greater than 10, print the first 10 characters, less than 10, and output a
Can recursion still play like this? Recursive implementation of minesweeping game
C language to achieve three chess? Gobang? No, it's n-chess