In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to initialize Servlet", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to initialize Servlet" this article.
Sometimes, we need to do some initialization work only when the service is started, rather than other more complex scheduled tasks, such as importing some data into memory, and so on, which can be well done with Servlet initialization. Initialization is done through the init () method in Servlet.
Create a Servlet class: AutoRunServlet.java, with the following code:
Public class AutoRunServlet extends HttpServlet {private static final long serialVersionUID = 1L; public void init () throws ServletException {System.out.println ("> > you can initialize something here > >"); String v = this.getInitParameter ("a"); Enumeration e = this.getInitParameterNames (); while (e.hasMoreElements ()) {System.out.println ("> > >" + e.nextElement ());}
Configure in web.xml:
The parameter value 1 b passed in by the package path 0 an of the AutoRunServlet AutoRunServlet// class
0 indicates the running priority of Servlet, and 0 indicates that Servlet is loaded and initialized as soon as the service is started.
Start the service (such as tomcat), and you can see the print out in the console:
You can initialize something here.
In addition, some kind of error may be reported when starting the service (tomcat):
Jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
This is generally because the Servlet package used in your project duplicates with the package in tomcat. I use tomcat 6.0.x. If I have a Servlet package in the project: servlet-api-2.5-6.1.14.jar, then an error will be reported at this time, because servlet-api.jar already exists under TOMCAT_HOME/lib, so the solution is to keep only one package. For example, I delete servlet-api- 2.5-6.1.14.jar under the project, and then restart the service without a problem.
These are all the contents of the article "how to initialize Servlet". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.