Difference and function between <context-param> and <init-param> (reproduced)

The function of <context-param>: The function
of <context-param> in the configuration of web.xml
1. When starting a WEB project, the container (such as: Tomcat) will read its configuration file web.xml. Read two Nodes: <listener></listener> and <context-param></context-param>

2. Next, the container creates a ServletContext (context), which will be shared by all parts of the WEB project.

3. The container will < context-param></context-param> is converted into a key-value pair and handed over to ServletContext.

4. The container creates a class instance in <listener></listener>, that is, creates a listener.

5. There will be contextInitialized( ServletContextEvent args) initialization method, in this method get ServletContext = ServletContextEvent.getServletContext();
context-param value = ServletContext.getInitParameter("context-param key");

6. After getting this context-param value, you You can do some operations. Note that your WEB project has not been fully started and completed at this time. This action will be earlier than all Servlets.
In other words, at this time, your key value in <context-param> The operation to be done will be executed before your WEB project is fully started.

7. Example. You may want to open the database before the project starts.
Then you can set the database connection method in <context-param>, and initialize the database connection in the listener class.

8. This listener is a class written by itself. In addition to the initialization method, it also has a destruction method. It is used to close Release resources before application. For example, the database connection is closed. For


example:
<!-- Load spring configuration file-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB -INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-

INF/jason-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org. springframework.web.context.ContextLoaderListener</listener-class>
</listener>


Another example: --->Customize context-param, and customize listener to get this information

<context-param>
    <param-name>urlrewrite< /param-name>
    <param-value> false</param-value>
</context-param>
<context-param>
    <param-name>cluster</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>servletmapping</param-name>
    <param-value>*.bbscs</param-value>
</context-param>
<context-param>
    <param-name>poststoragemode</param-name>
    <param-value>1</param-value>
</context-param>
<listener>
    <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
</listener>


public class SysListener extends HttpServlet implements ServletContextListener {

private static final Log logger = LogFactory.getLog(SysListener.class);

public void contextDestroyed(ServletContextEvent sce) {

   //Used to operate when the container is closed
}


//Used to operate when the container is opened

public void contextInitialized(ServletContextEvent sce) {
   String rootpath = sce.getServletContext().getRealPath("/ ");
   System.out.println("-------------rootPath:"+rootpath);

   if (rootpath != null) {
    rootpath = rootpath.replaceAll("\\\\" , "/");
   } else {
    rootpath = "/";
   }
   if (!rootpath.endsWith("/")) {
    rootpath = rootpath + "/";
   }
   Constant.ROOTPATH ​​= rootpath;
   logger.info("Application Run Path:" + rootpath);
   String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");
   boolean burlrewrtie = false;
   if (urlrewrtie != null) {
    burlrewrtie = Boolean.parseBoolean(urlrewrtie);
   }
   Constant.USE_URL_REWRITE = burlrewrtie;
   logger.info("Use Urlrewrite:" + burlrewrtie);
   其它略之....

   }

}
   /*最终输出
   -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Use Urlrewrite:true
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.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- The difference between param and init-param
There are two kinds of parameters that can be defined in web.xml:
(1) Parameters within the scope of the application are stored in the servletcontext and configured in web.xml as follows:
<context-param>
           <param-name>context/ param</param-name>
           <param-value>avalible during application</param-value>
</context-param>

(2) The parameters within the servlet scope can only be obtained in the init() method of the servlet. The configuration in .xml is as follows:
<servlet>
    <servlet-name>MainServlet</servlet-name>
    <servlet-class>com.wes.controller.MainServlet</servlet-class>
    <init-param>
       <param-name>param1</param-name>
       <param-value>avalible in servlet init()</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>

在servlet中可以通过代码分别取用:
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("The following two parameters param1 are stored in the servlet");
         System.out.println(this.getInitParameter("param1"));
         System.out.println("The following parameters are stored in the servlet ") in servletcontext;
        System.out.println(getServletContext().getInitParameter("context/param"));
      }
}

The first parameter in servlet can be passed getServletContext().getInitParameter("context/param")
The second through this.getInitParameter("param1") in the servlet's init() method.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988559&siteId=291194637