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

How to use Filter of Java Web

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use Filter of Java Web". In daily operation, I believe many people have doubts about how to use Filter of Java Web. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Filter of Java Web". Next, please follow the editor to study!

# # Overview of Servlet filter

The Servlet filter itself does not generate request and response objects, it only provides filtering.

Servlet filters can check Request objects and modify RequestHeader and Request contents before Servlet is called

After, servlet is called, check the Response object and modify the Response Header and Response contents. The web component that the Servlet filter is responsible for filtering can be Servelt, jsp, or html files.

The Filter API method states that when the public void init (FilterConfig filterConfig) web application starts, the web server will create an instance object of Filter, call its init method, read the web.xml configuration, and complete the initialization of the object, thus preparing for the interception of subsequent user requests (the filter object will only be created once, and the init method will only be executed once). Developers can obtain a FilterConfig object that represents the current filter configuration information through the parameters of the init method. Public void doFilter (ServletRequest, ServletResponse, FilterChain) is used to complete the actual filtering operation. When a customer requests access to the URL associated with the filter, the Servlet container will first call this method of the filter. The FilterChain parameter is used to access the subsequent filter void destroy () filter to execute this method before it is cancelled, releasing the resource public class LoginFilter implements Filter {public void init (FilterConfig config) throws ServletException {String site = config.getInitParameter ("Site") requested by the filter. System.out.println (site);} public void destroy () {} public void doFilter (ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {System.out.println (req.getServerName () + req.getServerPort ()); chain.doFilter (req,resp);}}

Web.xml

LoginFilter cn.java.servlet.LoginFilter Site www.google.com LoginFilter / *

Web.xml configuration instructions for each node

Specify a filter.

Used to specify a name for the filter, and the content of the element cannot be empty.

Element is used to specify the fully qualified class name of the filter.

The element is used to specify initialization parameters for the filter, and its child elements specify the name of the parameter and the value of the parameter.

In the filter, you can use the FilterConfig interface object to access the initialization parameters.

Element is used to set the resource that a Filter is responsible for intercepting. A resource intercepted by a Filter can be specified in two ways: the Servlet name and the request path for resource access

The child element is used to set the registered name of the filter. The value must be the name of the filter declared in the element

Set the request path intercepted by filter (URL style associated with the filter)

Specifies the name of the Servlet intercepted by the filter.

Specifies how the resource intercepted by the filter is called by the Servlet container, which can be one of REQUEST,INCLUDE,FORWARD and ERROR, and the default REQUEST. Users can set multiple child elements to specify multiple ways for Filter to intercept calls to resources.

The values that can be set by child elements and their meaning

REQUEST: when the user visits the page directly, the Web container will call the filter. If the target resource is accessed through the include () or forward () method of RequestDispatcher, the filter will not be called.

INCLUDE: this filter will be called if the target resource is accessed through the include () method of RequestDispatcher. Otherwise, the filter will not be called.

FORWARD: if the target resource is accessed through the forward () method of RequestDispatcher, then the filter will be called, otherwise, the filter will not be called.

ERROR: this filter will be called if the target resource is called through a declarative exception handling mechanism. Otherwise, the filter will not be called.

At this point, the study on "how to use the Filter of Java Web" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 270

*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

Internet Technology

Wechat

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

12
Report