In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 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 initialization order of Listener, Filter and Servlet". 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 is the initialization order of Listener, Filter and Servlet".
Listener, Filter and Servlet all have an initialization process, and the corresponding methods are:
ContextInitialized (ServletContextEvent arg0)
Init (FilterConfig filterConfig)
Init (ServletConfig config)
So what is their initialization order?
Listener > Filter > Servlet
TestServlet.java
Java code
Package com.cos; import java.io.IOException; import javax.servlet.GenericServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class TestServlet extends GenericServlet {@ Override public void init (ServletConfig config) {System.out.println ("Servlet initialization.") ;} @ Override public void service (ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {System.out.println ("Servlet service.") ;}}
TestFilter.java
Java code
Package com.cos; 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; public class TestFilter implements Filter {public void init (FilterConfig filterConfig) throws ServletException {System.out.println (Filter initialization. ") ;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println ("doFilter.") ; chain.doFilter (request, response);} public void destroy () {System.out.println ("Filter destruction.") ;}}
TestListener.java
Java code
Package com.cos; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class TestListener implements ServletContextListener {public void contextInitialized (ServletContextEvent arg0) {System.out.println ("Listener initialization.") ;} public void contextDestroyed (ServletContextEvent arg0) {System.out.println ("Listener destruction.") ;}}
Web.xml
Xml code
Sservlet-name > com.cos.TestServletservlet-class > servlet > sservlet-name > / loginurl-pattern > servlet-mapping > ffilter-name > com.cos.TestFilterfilter-class > filter > ffilter-name > / * url-pattern > filter-mapping > com.cos.TestListenerlistener-class > listener > web-app >
After starting tomcat, print out the following in the console:
Listener initialization.
Filter initialization.
Information: Server startup in 675 ms
It can be seen that the initialization of Listener is the earliest, followed by Filter. Both initializations are initialized before the container starts up.
Servlet was not initialized because no matching request came in.
The order of initialization is independent of the order of Listener, Filter and Servlet in web.xml.
When there are multiple Filter or multiple Servlet, whose mapping is in front and who initializes it first.
Thank you for your reading, the above is "what is the initialization order of Listener, Filter, Servlet". After the study of this article, I believe you have a deeper understanding of what the initialization order of Listener, Filter and Servlet is, 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.
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.