In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the working mode of Servlet filter". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what is the working mode of Servlet filter" together!
Servlet filters are pluggable Web components that allow us to implement pre-and post-processing logic in Web applications. Filters support basic request-processing features of servlets and JSP pages, such as logging, performance, security, session processing, XSLT transformations, and more.
Since J2EE 1.3, filter support has been added to the Servlet 2.3 specification. Filters intercept requests and responses to targeted resources. There are four ways filters work. Let's take a look at how these four filters work:
1. Request filter
The way this filter works is relatively simple, and we often encounter it, as shown in the following figure:
Here is how to configure web.xml files:
myFilter xx.MyFilter myFilter Target Resource 1
Let's change the configuration of the web.xml file as follows:
myFilter xx.MyFilter myFilter Target Resource 1 myFilter Target Resource II
That is to say, this filter filters both target resource 1 and target resource 2, and then when target resource 1 is accessed, we forward the request to target resource 2. How does this filter work? As shown below:
We can see that when we visit target resource 1, the filter intercepts the request, and then forwards it to target resource 1, and then forwards it to target resource 2. From the figure, we can see that the filter does not intercept the request forwarded to target resource 2, but we have configured the filter to filter target resource 2 in the web.xml file. Why does it not play a filtering role?
The answer is that target resource 1 is directly accessed by the client, and target resource 2 is forwarded, so the filter cannot filter target resource 2. If you go directly to target resource two, you'll see that the filter works.
Our web.xml file configuration above is equivalent to:
myFilter myFilter xx.MyFilter myFilter Target Resource A REQUEST myFilter Target Resource II REQUEST
This configuration means that the filter will only work if the target resource is accessed directly, and requests forwarded to the target resource will be ignored.
What if I want to filter requests forwarded to target resource two? The answer to that is, the next filter, the forward filter.
2. Forward filter
We modify the configuration of the web.xml file as follows:
myFilter myFilter xx.MyFilter myFilter Target Resource A REQUEST myFilter Target Resource II FORWARD
Work as shown below:
Let's look at the configuration method of filtering target resource 2. In this case, the filtering method is forward, that is, the request forwarded to target resource 2 is filtered. If target resource 2 is directly accessed, the filter will not work.
3, include filters
Once you understand the forward filter, the include filter is easy to understand. By:
myFilter Target Resource II INCLUDE
This means that requests containing target resource two are filtered. If target resource two is accessed directly, this filter will not work.
Include contains the following statements:
Action in JSP page: directive contains when this filter does not work.
4. Error filter
When we visit a web target resource, if the server does not find the target resource, then the server will give a 404 error code. If we define a page for the 404 error code, then the page will be called when the 404 error occurs, see the following configuration of the web.xml file:
myFilter /error.jsp ERROR 404 /error.jsp
When we access a file that doesn't exist, we access error.jsp. However, filters are configured to filter error pages, so the filter accepts the request first and then forwards it to error.jsp.
If we visit a page that already exists, will error.jsp be called? If there is a response.sendError (404,"Something went wrong! "); then the error page will still be called and the filter will work.
Thank you for reading, the above is "Servlet filter work is what" content, after the study of this article, I believe we have a deeper understanding of Servlet filter work is what this problem, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.