A web project web.xml context-param role of

web.xml configuration arranged effect

  1. WEB start a project, containers (such as: Tomcat) will read its configuration file web.xml read two nodes: and
    2. Then, create a container ServletContext (context), all parts of this project will be WEB sharing this context.
    3. the key-value pair into the vessel, and to the ServletContext.
    class instance created in the container 4., i.e., creates a listener.
    5. there will contextInitialized (ServletContextEvent args) listening initialization method, in this method = ServletContextEvent.getServletContext obtain the ServletContext ();
    context-param value = ServletContext.getInitParameter ( "context-param key");
    6. get this value context-param after, you can do something a note. this time your WEB project has not been fully completed to start. this action will be earlier than all of the Servlet.
    in other words, this time you do on the key operation and will be in full before you start the project WEB execution.
    7. For example you may want to open before the start of the project database.
    so here you can set up connections in the database, in class listening to initialize the database Connect
    8. This monitor is to write a class, in addition to the initialization method, it also destroyed a method for releasing resources before closing the application example, close the database connection...
    Such as:
SysListener] - [INFO] Use Cluster: false 2009-06-09 21: 51: 46,526 [com.laoer.bbscs.web.servlet.SysListener] - [INFO] SERVLET MAPPING: * bbscs 2009-06-09 21:. 51: 46,573 [com.laoer.bbscs.web.servlet.SysListener] - [INFO] Post Storage Mode: 1 * / context-param and the init-param difference web.xml which can define two parameters: (1) application range the parameters to be kept at the servletcontext, in web.xml configured as follows: context / param avalible during application (2) servlet within a parameter range, only () acquires the init method of the servlet, the configuration in web.xml as follows: MainServlet com.wes.controller.MainServlet param1 avalible in servlet init () 0 can respectively access the servlet by the code: package com.wes.controller; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet ; public class MainServlet extends HttpServlet ... {public MainServlet () ... {super ();} public void init () throws ServletException ... {System.out.println ( "param1 following two parameters in the servlet stored in "); System.out.prin tln (this.
Published 25 original articles · won praise 22 · views 3657

Guess you like

Origin blog.csdn.net/weixin_42443419/article/details/93529215