In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article analyzes "the Filter structure of J2EE and how to set up a filter". The content is detailed and easy to understand. Friends who are interested in "the Filter structure of J2EE and how to build a filter" can follow the editor's train of thought to read it in depth. I hope it will be helpful to you after reading. Let's follow the editor to learn more about the Filter structure of J2EE and how to build filters.
J2EE provides a special kind of Servlet, which is Filter. It is not a separate network component because it does not generate request and response information, and it must be attached to other network components. So we use it to complete many kinds of work, such as information coding conversion, data encryption, authentication, data compression, log recording and so on. Next, let's take a look at the structure and specific application methods of Filter, and explain it with examples.
I. the structure of Filter
The Filter we wrote ourselves must implement the javax.servlet.Filter interface, which contains only three main methods:
Init ()-initializes the filter, an instance of its input parameter javax.servlet.FilterConfig, where you can initialize the FilterConfig to be used for filtering. This method is called automatically by the Web container.
DoFilter ()-performs specific filtering operations, this method uses javax.servlet.ServletRequest request information, javax.servlet.ServletResponse response information, and javax.servlet.FilterChain filter chain. Filter chain, all filters in the Web application will form a chain, and programs that meet the filter criteria will execute the filters in all chains in the defined order. Calling the doFilter (javax.servlet.ServletRequest, javax.servlet.SerletResponse) method of FilterChain in this method can be passed to the next filter in the chain.
Destory ()-destroys the filter, where you can release used resources, such as setting FilterConfig to null in the filter.
Second, set up the filter in the Web program
1. Establish a custom Filter program to implement javax.servlet.Filter interface. The following is a Filter for encoding and transforming the request information:
2. Configure the web.xml file. If you want Filter to play other roles in the program, you must configure it in web.xml, and the filter above corresponds to the configuration file:
< filter>Element is information related to configuring Filter
< filter-name>Specify the name of the Filter, which can be specified at will, which is actually equivalent to a description of the Filter.
< filter-class>The complete classpath (package path) of Filter. Class).
< init-param>Specifies the initial value of the parameter used by Filter, where is the parameter name
< param-value>It's a parameter value. In Filter, we can get it using FilterConfig.getInitParameter (String), where String represents the value of the parameter name, for example, get the value of encoding in the above Filter: filterConfig.getInitParameter ("encoding")
< filter-mapping>Used to specify the URL style that passes through the Filter.
< filter-name>It's the name of Filter, and this one should correspond to it.
< filter>Medium
< filter-name>Consistent.
< url-pattern>Is the style of URL, which defines the styles that all URL passing through this Filter should satisfy, such as
< url-pattern>/ *
< /url-pattern>It is specified that all url will pass through this Filter.
After completing the above process, a completed Filter is defined, and the filter establishment is complete. We can add auxiliary code for testing.
Above we mentioned the concept of filter chain, which has a problem of execution order, so this order is also set in web.xml, where the order specifies this order. In the example, a total of two filters are included, and the test code is included, and we can see the difference between the results before and after adjustment by adjusting the order of the two filters.
So much for sharing about the Filter structure of J2EE and how to set up a filter. I hope the above content can improve everyone. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.