Servlet -- get Initialization Configuration Information

Source: Internet
Author: User

Servlet -- get Initialization Configuration Information

This content mainly involves playing with an interface: ServletConfig. First, let's look at the API.
Definition:
Public interface ServletConfig
This interface defines an object through which the Servlet engine configures a Servlet and allows the Servlet to obtain a description of its ServletContext interface. Each ServletConfig object corresponds to a unique Servlet.
Method:
1. getInitParameter public String getInitParameter (String name );
This method returns a String containing the initialization parameter specified by the Servlet. If this parameter does not exist, the return value is null.

2. getInitParameterNames public Enumeration getInitParameterNames ();

This method returns a list of String objects, including all the Servlet initialization parameter names. If the Servlet does not have initialization parameters, getInitParameterNames returns an empty list.
3. getServletContext public ServletContext getServletContext ();

The ServletContext object of the Servlet is returned.

 

Now let's take a look at the implementation of this interface in Tomcat. Tomcat uses the facade mode, which encapsulates ServletConfig. the methods provided in this interface are the methods of the object ServletConfig. The source code is posted below:

 

package org.apache.catalina.core;import java.util.Enumeration;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;public final class StandardWrapperFacade implements ServletConfig{private ServletConfig config = null;private ServletContext context = null;public StandardWrapperFacade(StandardWrapper config){super();this.config = (ServletConfig) config;}public String getServletName(){return config.getServletName();}public ServletContext getServletContext(){if (context == null){context = config.getServletContext();if ((context != null) && (context instanceof ApplicationContext))context = ((ApplicationContext) context).getFacade();}return (context);}public String getInitParameter(String name){return config.getInitParameter(name);}public Enumeration getInitParameterNames(){return config.getInitParameterNames();}}


 

OK. Now let's get down to the point and take a closer look at this interface.
In some cases, the Servlet may need to access the Servlet container or use the Servlet container to access external resources. Therefore, the Serlvet engine must pass the Objects representing the Servlet container to the Servlet. In addition, the friendly name and initialization parameters set for a Servlet in the web. xml file also need to be passed to the Servlet.


The Servlet Engine encapsulates the Servlet container object (ServletContext) and Servlet configuration parameter information into an object called ServletConfig, and then passes it to the Servlet when initializing the Servlet instance object. The ServletConfig interface is used to define the methods that the ServletConfig object needs to provide externally, so that the Servlet program can call these methods to obtain relevant information. The Servlet Engine calls the Servlet instance object's init (ServletConfig config) method to pass the ServletConfig object to the Servlet. The Servlet. getServletConfig () method must return reference to the ServletConfig object passed in by the init (ServletConfig config) method.


Note that two items are encapsulated here: one is the object representing the Servlet container, and the other is the configuration parameter information of the Servlet. How can we differentiate these two items?
1. ServletConfig is an object for servlet to access configuration data. It is created by the container and exclusive to each servlet. Valid only for this servlet. The ServletConfig object of one servlet cannot be accessed by another servlet. It is mainly used to read the initial Servlet Information configured in web. xml and cannot be shared by other servlets. It can also be used to access ServletContext.
2. ServletContext is the object that multiple servlets share data. It is valid for any servlet in the same Web application at any time. Corresponding to the running environment of the Web Application, it can be used to read the initial information of the Application configured in web. xml, write logs, and share data. ServletContext is shared by all servlets. It can be understood as a global object in the true sense. For details about this object, refer to the following:

The Servlet engine creates a corresponding ServletContext object for each WEB application. The ServletContext object is included in the ServletConfig object. The reference of the ServletContext object can be returned by calling the ServletConfig. getServletContext method. Because all servlets in a WEB application share the same ServletContext object, the ServletContext object is called the application object ).
Function: obtains initialization parameters of WEB applications, logs, application domain attributes, resource files, local paths mapped to virtual paths, and WEB applications, other methods of ServletContext.


Through the above understanding, I will not go into details about some concepts. What I want to talk about now is why I want to play with these two things. Why should I go to the configuration information for Servlet initialization? The advantage of setting initialization parameters for WEB applications is that some parameter information can be changed without modifying the Servlet Source program. That is to say, to prevent hard coding and to prevent subsequent maintenance from modifying the code.

OK. Now we use two examples to write a Servlet to implement the Servlet interface and implement two functions.

1. Use SerlvetConfig to obtain the Servlet's To obtain the Servlet initialization parameters.

 

package linkin;import java.io.IOException;import java.util.Enumeration;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class LinkinServlet implements Servlet{public LinkinServlet(){System.out.println(LinkinServlet...);}@Overridepublic void destroy(){System.out.println(destroy...);}@Overridepublic ServletConfig getServletConfig(){return null;}@Overridepublic String getServletInfo(){return null;}@Overridepublic void init(ServletConfig servletConfig) throws ServletException{String name = servletConfig.getInitParameter(name);System.out.println(name);Enumeration
 
   enumeration = servletConfig.getInitParameterNames();//name,yourNamewhile (enumeration.hasMoreElements()){String value = (String) enumeration.nextElement();System.out.println(value + : + servletConfig.getInitParameter(value));}}@Overridepublic void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException{System.out.println(service...);}}
 

 
 
  
   
    LinkinServlet
   
   
    linkin.LinkinServlet
   
   
    
     name
    
    
     LinkinPark...
    
   
   
    
     yourName
    
    
     NightWish...
    
   
  
  
   
    LinkinServlet
   
   
    /LinkinServlet
   
  
 

 

Here is an optimization. The current ServletConfig object is a parameter of the init () method. That is to say, the initialization parameters of the Servlet can only be obtained in this method, what if I want to obtain the relevant initialization parameters in other methods?

It's easy to extend the scope of the ServletConfig object. Define the ServletConfig object as a class member variable, and assign values to the init object. GenericServlet is actually doing this, but its ServletConfig object is modified using private.


2. Use ServletContext to obtain Parameters to obtain the initialization parameters of the entire web application.

package linkin;import java.io.IOException;import java.util.Enumeration;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class LinkinServlet implements Servlet{public LinkinServlet(){System.out.println(LinkinServlet...);}@Overridepublic void destroy(){System.out.println(destroy...);}@Overridepublic ServletConfig getServletConfig(){return null;}@Overridepublic String getServletInfo(){return null;}@Overridepublic void init(ServletConfig servletConfig) throws ServletException{System.out.println(servletConfig.getServletContext().getInitParameter(name));Enumeration
 
   enumeration = servletConfig.getServletContext().getInitParameterNames();while (enumeration.hasMoreElements()){String value = (String) enumeration.nextElement();System.out.println(value + : + servletConfig.getServletContext().getInitParameter(value));}}@Overridepublic void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException{System.out.println(service...);}}
 

 
 
  
   
    name
   
   
    LinkinPark...
   
  
  
   
    name1
   
   
    LinkinPark1...
   
  
  
   
    LinkinServlet
   
   
    linkin.LinkinServlet
   
  
  
   
    LinkinServlet
   
   
    /LinkinServlet
   
  
 




 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.