Servlet Context and Config

Source: Internet
Author: User

For web containers, the Servlet Context interface defines a Servlet environment object, which defines a Servlet view on the Servlet engine. By using this object, Servlet can record events, obtain resources, and obtain the engine class from Servlet.

The Servlet container loads the web application at startup and creates a unique Servlet context object for each web application. You can regard the Servlet Context as the shared memory of the server-side component of a Web application, the Servlet Context can store shared data. It provides four methods to read and set shared data. For more information, see the api help documentation.

In addition, Servlet Context objects are destroyed only when web applications are disabled. Servlet Context exists independently for different web applications.

A web application consists of a collection of web components such as jsp, Servlet, and javabean. Each web application and container has a background object, while javax. servlet. the Servlet Context interface provides a way to access this background object. You can use the getServlet Context () method of a Servlet instance to obtain the background object in which the Servlet runs. From this background object, you can access the following information resources:
1. initialization parameters
2. objects stored in the background
3. resources associated with the background
4. Logs

My own summary of Servlet Context is:
The Servlet Context is the Servlet container. the methods provided in the Servlet can be used in all servlets of the same web application. For the config object, compared with the context, there are great limitations.

After the Servlet Config object is instantiated in the Servlet, it is valid for access by any client at any time. However, the Servlet Config object of a Servlet cannot be accessed by another Servlet. That is to say, after the Servlet declaration, the Servlet Config object can only be accessed within the Servlet and is a persistent internal variable.

The following example provides a deep understanding:

Generally, the configuration of the entire application should be configured as the Servlet Context parameter, such as Character Set setting, to avoid "Hard encoding.

 
 
  1. <web-app> 
  2. .................  
  3. <init-param> 
  4. <param-name>charset</param-name>   
  5. <param-value>GB2312</param-value>   
  6. </init-param> 
  7. .................  
  8. </web-app> 

Note that the preceding format is only 2. Standard Format after 0, old container engine) is configured in the format of the service provider. Note that its parent element should be <web-app>, that is, it acts on an application.

If there is only one specific Servlet parameter to be set and other servlets cannot be shared, it should be configured as the Servlet Config parameter. For example, an absolute directory should be used for a Servlet reading attachments, other servlets do not use the following:

 
 
  1. <servlet> 
  2. <servlet-name>GetAtt</servlet-name> 
  3. <servlet-class>mail.GetAttServlet</servlet-class> 
  4. <init-param> 
  5. <param-name>absPath</param-name>   
  6. <param-value>/usr/mail/ax/axman/Maildir/</param-value>   
  7. </init-param> 
  8. </servlet> 

Needless to say, because name and class have been specified in the <Servlet> label, that is, path can be obtained only in mail. GetAttServlet \ rServlet, and other servlets cannot be obtained.

We have discussed the concept of these two attributes. Let's take a look at how to obtain the parameters of these two objects:

Access Servlet Config parameters:
First, retrieve the Servlet Config object and call its getInitParameter (); method. To access the Servlet Config object, jsp directly uses the config built-in object, but because the Servlet compiled by your JSP will not be added to the web. in xml, so jsp is generally not used to obtain the Servlet configuration parameters after the JSP is compiled. There are two methods to get the Servlet Config object in Servlet:

In the inii () method, it is obtained through the init overload method.

 
 
  1. .....  
  2. public class Test extends HttpServlet   
  3. {  
  4. ServletConfig config;  
  5. public void init(ServletConfig config) throws ServletException {  
  6. this.config = config;  
  7. }  
  8. ..................  

Then, you can access the config object in the following method. Note that to ensure that the config object of the Servlet can be retrieved from the constructor, the constructor of the parent class should be called:

 
 
  1. .....  
  2. public class Test extends HttpServlet   
  3. {  
  4. ServletConfig config;  
  5. public void init(ServletConfig config) throws ServletException {  
  6. super.init(config);  
  7. this.config = config;  
  8. }  
  9. ..................  

When the getServlet Config () method is used directly, the advantage of this is that you do not have to manually pass the attribute, and you can
.

There is also a third method. We need to implement some interfaces by ourselves. We will not introduce them here as a general discussion.
To access the Servlet Context object, you only need to get its parameters from the existing Servlet Config object getServlet Context), and then \ r can call its getInitParameter () method.

The scope of the Servlet Context object is larger than that of the Servlet Config object. Why should we get the Servlet Context object from the Servlet Config object? I personally think: The container saves many Servlet Context objects. Which of the following \ r is the container used for the request? Take the one that contains the Servlet Config information for you, that is, take the parent object of the Servlet Config object. As if HttpSession is to be obtained from requset, it is to get the session object containing the current request object to you. This is just my personal idea and I have not had time to see the specific implementation. That's all about it.

  1. DoFilter method in Servlet
  2. Configure Servlet Filter
  3. Install Servlet and JSP development tools
  4. Java Servlet getting started
  5. What is a Servlet filter?

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.