Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the conceptual characteristics of Servlet filter

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the conceptual characteristics of Servlet filter". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the conceptual characteristics of Servlet filter".

First, the concept of Servlet filter:

The ◆ Servlet filter, defined in Java Servlet Specification 2.3, can check and modify the request and response objects of the Servlet container.

The ◆ Servlet filter itself does not produce request and response objects, it can only provide filtering. Servlet expiration can check the Request object and modify the Request Header and Request content before the Servlet is called; check the Response object and modify the Response Header and Response content after the Servlet is called.

Servlet expiration the Web component responsible for filtering can be Servlet, JSP, or HTML files.

Second, the characteristics of Servlet filter:

◆ Servlet filters can check and modify ServletRequest and ServletResponse objects

A ◆ Servlet filter can be specified to be associated with a specific URL and will be triggered only when a customer requests access to the URL

◆ Servlet filters can be concatenated together to create a pipelining effect that collaborates to modify request and response objects

Third, the function of Servlet filter:

◆ queries the request and takes action accordingly.

◆ blocks the request-response so that it cannot be delivered further.

◆ modifies the header and data of the request. Users can provide custom requests.

◆ modifies the header and data of the response. Users can do this by providing a customized version of the response.

◆ interacts with external resources.

Fourth, the application of Servlet filter:

◆ authentication filtering

◆ login and audit filtering

◆ image conversion filtering

◆ data compression and filtering

◆ encryption filtering

◆ token filtering

◆ resource access trigger event filtering

◆ XSL/T filtering

◆ Mime-type filtering

Fifth, the composition of Servlet filter interface:

All Servlet filter classes must implement the javax.servlet.Filter interface. This interface contains three methods that the filter class must implement:

◆ init (FilterConfig):

This is the initialization method of the Servlet filter, which will be called after the Servlet container creates the Servlet filter instance. In this method, you can read the initialization parameters of the Servlet filter in the web.xml file

◆ doFilter (ServletRequest,ServletResponse,FilterChain):

This method completes the actual filtering operation, and when a customer requests access to the URL associated with the filter, the Servlet container will first call the filter's doFilter method. The FilterChain parameter is used to access subsequent filters

◆ destroy ():

The Servlet container calls this method before destroying the filter instance, which frees the resources occupied by the Servlet filter

6. Steps to create a Servlet filter:

Implementing javax.servlet.Filter Interface with ◆

◆ implements the init method and reads the initialization function of the filter

◆ implements the doFilter method to complete the response to the request or filter

◆ calls the doFilter method of the FilterChain interface object to pass the request or response to the subsequent filter

◆ destroy filter

7. Filter the request by Servlet filter:

The ◆ Servlet container creates a filter instance

The ◆ filter instance calls the init method to read the initialization parameters of the filter

The ◆ filter instance calls the doFilter method to determine whether the request is legal according to the value of the initialization parameter.

◆ blocks the request if it is illegal

◆ if the request is valid, call the chain.doFilter method to pass the request to the subsequent

8. Servlet filter for response filtering:

◆ filter intercepts client requests

◆ re-encapsulates ServletResponse to provide a user-defined output stream in the encapsulated ServletResponse

◆ passes the request to subsequent

◆ Web components generate responses

◆ obtains the user-defined output stream from the encapsulated ServletResponse

◆ writes the response content to the buffer stream through a user-defined output stream

◆ clears the buffer stream after modifying the response content in the buffer stream, and outputs the response content

IX. Release of Servlet filter:

When ◆ publishes Servlet filters, you must add < filter > elements and < filter-mapping > elements to the web.xml file.

The ◆ filter element defines a filter:

/ / attribute meaning filter-name specifies the name of the filter filter-class specifies the class name of the filter init-param provides initialization parameters for the filter instance, and there can be multiple

The ◆ filter-mapping element is used to associate a filter with a URL:

/ / attribute meaning filter-name specifies the name of the filter url-pattern specifies the URL associated with the filter, and indicates all URL for "/ *"

Matters needing attention in the use of Servlet filters

◆ since Filter, FilterConfig and FilterChain are all located under the javax.servlet package and are not unique to the HTTP package, the request, response object ServletRequest and ServletResponse must be converted to HttpServletRequest and HttpServletResponse before being used.

◆ configures Servlet and Servlet filters in web.xml. You should declare the filter element before the Servlet element

If ◆ wants to observe the logs generated by the filter in Servlet, make sure that the following < logger > element is configured in the < host > element corresponding to the localhost of server.xml:

< Logger className = "org.apache.catalina.logger.FileLogger" directory = "logs" prefix = "localhost_log." suffix= ".txt" timestamp = "true" / > Thank you for reading. The above is the content of "what are the conceptual characteristics of Servlet filter?" after the study of this article, I believe you have a deeper understanding of what the conceptual characteristics of Servlet filter have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report