Example analysis of Filter in JavaWeb Servlet
This article mainly introduces the relevant information about the detailed explanation of Filter filter in JavaWeb Servlet. I hope this article can help everyone and let everyone fully understand how to use it. Friends in need can refer to
JavaWeb Servlet Detailed explanation of Filter filter
1. Brief description
Filter filter filters all web resources of the web server to achieve some special functions (Permission access control, filtering sensitive words, compressing response information). Filter can check and modify the requests and responses of the Servlet container. It cannot generate request requests and responses by itself. It only provides filtering function. (Before the Servlet is called, the Request object is checked to modify its related information. The Servlet is called. After checking the Response and modifying its related information), the Filter object is resident on the server.
2.Lifecycle
3.chain filter chain
The two filters play different filtering roles. The server will assemble them into a chain in the order defined by the filters in web. It can be interrupted at any time during the execution process. As long as chain.doFilter() is not executed, subsequent filters and requested content will not be executed.
4. Filter configuration
(1). Inherit the Filter interface and override the doFilter() method (2). Configuration of web.xml to filter operations that meet the requirements
About url-pattern configuration instructions>>
/* * MyFilter.java * 过滤器 */ package com.baidu.web; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyFilter implements Filter{ @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //对请求request进行处理 HttpServletResponse resp = (HttpServletResponse)response; HttpServletRequest req = (HttpServletRequest) request; String username = (String) req.getSession().getAttribute("username"); if(req.getRequestURI().contains("Buy") || req.getRequestURI().contains("collect")){ if(username==null || username.equals("")){ //满足过滤要求,进行处理 req.getRequestDispatcher("/login.jsp").forward(req, resp); return; } } chain.doFilter(req, resp); //对相应response进行处理 } @Override public void init(FilterConfig filterConfig) throws ServletException { //filterConfig,通过filterConfig的getServletContext() 方法可以获得 ServletContext } }
<filter> <filter-name>filter</filter-name> <filter-class>com.baidu.web.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The above is the detailed content of Example analysis of Filter in JavaWeb Servlet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. JavaWeb Security Basics 1. What is code auditing? In layman’s terms, Java code auditing is to discover security issues in the Java application itself by auditing Java code. Since Java itself is a compiled language, even if there are only class files We can still audit Java code. For uncompiled Java source code files, we can read the source code directly, but for compiled class or jar files, we need to decompile them. Java code auditing itself is not very difficult. As long as you are proficient in the auditing process and common vulnerability auditing techniques, you can complete the code auditing work relatively easily. But the way of Java code auditing is not just to use

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

The full name of Servlet is "Java Servlet", which means small service program or service connector in Chinese. It is a program running on a Web server or application server. It serves as a request from a Web browser or other HTTP client and a database on the HTTP server or The middle layer between applications. Servlet has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content.
![How to solve the '[Vue warn]: Failed to resolve filter' error](https://img.php.cn/upload/article/000/887/227/169243040583797.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

JavaServlet can be used for: 1. Dynamic content generation; 2. Data access and processing; 3. Form processing; 4. File upload; 5. Session management; 6. Filter. Example: Create a FormSubmitServlet to handle form submission, taking name and email as parameters, and redirecting to success.jsp.

There are two ways to implement distributed session management in JavaServlet: 1. Session replication: Copy session data to each server. 2. Session distribution: Use a centralized storage service to store session data and access it from multiple servers. The specific implementation methods are: session replication configures true in the web. session data.

JavaWeb development technology stack: Master JavaEE, Servlet, JSP, Spring and other technologies used for Web development. With the rapid development of the Internet, in today's software development field, the development of Web applications has become a very important technical requirement. As a widely used programming language, Java also plays an important role in the field of Web development. The JavaWeb development technology stack involves multiple technologies, such as JavaEE, Servlet, JSP, Spr

Servlet is a very commonly used technology in Java Web application development. However, some Servlet errors will inevitably occur during the development process. How to solve and avoid Servlet errors has become a top issue for many Java developers. This article will introduce some common Servlet errors and their solutions based on personal experience and related information. ClassNotFoundException When we try to load a class, if the class does not exist or cannot be accessed by the system,
