JavaWeb project start-up process and ServletContext

  The official name of the ServletContext called Servlet context, the server will create an object for each project, the object is the ServletContext . A project has only one ServletContext object, all servlet in the project share this object, it is also called the global application sharing objects.

A, web.xml loading process (step):

  1. Start the web project, containers (such as Tomcat, Apache) it will be to read the two nodes in the configuration file web.xml, context-param and listener.
  2. Then, the container will create a ServletContext (also known as: Servlet context), the range of applications that the whole project can use this WEB Servlet context.
  3. Container <context-param> into key-value pairs and to ServletContext.
  4. Container creating instances of <listener>, i.e. creates a listener. (Note: listener classes can be defined from the class definition, but must need to inherit ServletContextListener).
  5. In the monitor will contextInitialized (ServletContextEvent args) initialization method, in this method: ServletContext = ServletContextEvent.getServletContext (); context-param value = ServletContext.getInitParameter ( "context-param key"); in this class there must also be a contextDestroyed (ServletContextEvent event) destruction methods. For releasing resources before closing the application, such as database connection is closed.
  6. After obtaining the value of context-param, you can do some of the operations. Note that 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, at this time, you <context-param> The key to do the operation, to be performed before you start the WEB project completely.
  7. For example you may want to open before the start of the project database. This can be provided in the connection database <context-param>, the connection initialization of the database listener class.

Supplementary information: ServletContext, is a global space to store information, the server starts, it exists, the server shuts down, it was released. request, a user may have multiple; session, a user selection; servletContext, a common to all users. So, in order to save space, improve efficiency, ServletContext, we should have to put, it is important that all users need to share the security thread is some information. For example, a shopping site, users access to detailed product information to, if placed in session domains, each user must access the database only once, so that efficiency is too low; while in the ServletContext, a server is started, it will access the product information database into the database, so that all users can access only the information items by context.

Two, web.xml node load order:

  • Loading sequence web.xml nodes successively in web.xml regardless of their position, i.e. will not <filter> write <context-param> can be loaded in front of <filter>.
  • Also referred to above, <context-param> ServletContext of key-value pairs for providing that the application context information. The listener, servlet peers in the initialization process will be used in the context information, so in the end we come to the loading order web.xml node be: context-param-> listener-> filter-> servlet.
  • For certain types of configuration node it has is required location. In servlet instance, the configuration node is associated with a servlet servlet-mapping, to have the same configuration section servlet-name of servlet and for servlet-mapping, servlet-mapping must be defined in the servlet, or when resolving the servlet-mapping, it's servlet-name has not been defined. When the web servlet container starts to initialize each time, in the order appearing servlet configuration section initialized.
  • Final Conclusions: web.xml loading sequence is: [context-param -> listener -> filter -> servlet -> spring], while the sequence between the actual procedure of the same type when the node is called the order of the corresponding mapping the calling.

Guess you like

Origin www.cnblogs.com/1102whw/p/11298602.html