Access to ServletConfig and ServletContext

  • 2020-05-30 20:05:34
  • OfStack

1 generally speaking, ServletContext objects should be used for the configuration of the entire application in order not to use "hard coding".

If only one specific Servlet parameter needs to be set, and other Servlet cannot access it, then ServletConfig() should be used for 1;

PS: when using the ServletConfig object, in the init() method, 1 must initialize the ServletConfig object with the super class.


public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
        
        //TODO
    }

Let's take a look at each one:

1. ServletContext object

< context-param > Element: sets the Context starting parameter

In web.xml, you can take advantage of it < context-param > Element to define the Context starting parameter, which contains two child elements:

n < param-name > : defines the starting parameter name of Context

n < param-value > : defines the starting parameter value of Context

The following is a < context-param > Element. In this example, we define two Context starting parameters:

n driver_type: name of the JDBC driver to be used by the Web application

n url: target database location


<web-app>

   <context-param>

     <param-name>driver_type</param-name>

     <param-value>oracle.jdbc.driver.OracleDriver</param-value>

   </context-param>

   <context-param>

    <param-name>url</param-name>

    <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value>

  </context-param>

</web-app>

There are two ways to access the Context start parameter:

Table 1 methods used in the ServletContext interface to access the Context start parameters

方法名称

回传类型

用 途

getInitParameter()

String

取得某个Context起始参数值

getInitParameterNames()

java.util.Enumeration

取得所有Context起始参数

1. The getServletConfig() method is first called to obtain the ServletConfig object, and then the getServletContext() method defined by the ServletConfig interface is used to obtain the ServletContext object.


ServletConfig config = getServletConfig();  
ServletContext context = config.getServletContext(); 


String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");

2. Directly call the getServletContext() method to get the ServletContext object.



ServletContext context = getServletContext();
         
// Gets the configured parameters 
String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");
// Get the current WebApp The path of the 
String path=context.getRealPath("/");

2, ServletConfig object

< init-param > Element: sets the init starting parameter

In web.xml, you can use it < init-param > Element to define the Config starting parameter, which contains two child elements:

n < init-name > : defines the starting parameter name of Config

n < init-value > : defines the starting parameter value of Config

The following is a < init-param > Element. In this example, we define two Config starting parameters:

n driver_type: JDBC driver name to be used by Web application

n url: target database location


<web-app>
  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.simon.test.servlet.initparam.testServlet</servlet-class>
    
    <init-param>
       <param-name>driver_type</param-name>
       <param-value>oracle.jdbc.driver.OracleDriver</param-value>
  </init-param>

     <init-param>
      <param-name>url</param-name>
      <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value>
    </init-param>
      
  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
  </servlet-mapping>
</web-app>

In the init() method, you should:


public void init(ServletConfig config) throws ServletException
  {
        // You have to inherit super Of the class init() methods 
    super.init(config);
        
    String filename=getServletConfig().getInitParameter("config-file");
    
     //TODO
}


Related articles: