Learning note 4_servletcontext (sharing data between dynamic resources for the entire web app)

Source: Internet
Author: User

ServletContext (important)

A project has only one ServletContext object!

We can get this unique object in n multiple servlets and use it to pass data to multiple Servlets!

With Heaven and earth!!! This object was created when Tomcat was started and will die when Tomcat is closed!

1 ServletContext Overview

The server creates a ServletContext object for each app:

L ServletContext object is created when the server is started;

The destruction of the ServletContext object is done when the server shuts down.

The purpose of the ServletContext object is to share data between dynamic resources throughout the WEB application! For example, in Aservlet, you save a value to the ServletContext object, and then you can get the value in Bservlet, which is the shared data .   

2 Get ServletContext

L Servletconfig#getservletcontext ();

L Genericservlet#getservletcontext ();

L Httpsession#getservletcontext ()

L Servletcontextevent#getservletcontext ()

[C1]------------[C1] Less than!

Get the ServletContext object in the servlet:

L in void init (servletconfig config): servletcontext context = Config.getservletcontext ();

The Getservletcontext () method of the ServletConfig class can be used to obtain the ServletContext object;

Get the ServletContext object in Genericeservlet or HttpServlet:

L Genericservlet class has Getservletcontext () method, so it can be obtained directly using This.getservletcontext ();

  

public class Myservlet implements Servlet {

public void init (ServletConfig config) {

ServletContext context = Config.getservletcontext ();

}

...

}

public class Myservlet extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response) {

ServletContext context = This.getservletcontext ();

}

}

3 The function of the domain [C2] Object--------------The [C2] domain object is used to pass data across multiple Servlets!!!

 ServletContext is one of the four major domain objects of Javaweb:

    1. L PageContext;
    2. L ServletRequest;
    3. L HttpSession;
    4. L ServletContext;

All domain objects have the ability to access data because there is a map inside the domain object to store the data, and here's how the ServletContext object can manipulate the data:

1. void SetAttribute (String name, object value): Used to store an object or to store a domain property.

For example: Servletcontext.setattribute ("xxx", "xxx"), a domain property is saved in ServletContext, the domain property name is xxx, and the value of the domain property is xxx.

Note that if the method is called multiple times and the same name is used, the last value is overwritten, which is the same as the map;

2, Object getattribute (String name): Used to get the data in the ServletContext, the current need to go to store before the row,

For example: String value = (string) servletcontext.getattribute ("xxx"); Get the domain property named xxx;

3. void RemoveAttribute (String name): Used to remove domain properties from ServletContext.

If the domain attribute specified by the parameter name does not exist, then this method does nothing;

4, enumeration Getattributenames (): Gets the names of all domain properties;

4 getting the application initialization parameters

The servlet can also get initialization parameters, but it is a local parameter, that is, a servlet can only get its own initialization parameters, cannot get others, that is, the initialization parameters for only one servlet prepared!

L can configure common initialization parameters for all Servlets! This requires the use of ServletContext to use!

You can also use ServletContext to get the app initialization parameters configured in the Web. xml File! Note that the application initialization parameters are different from the servlet initialization parameters:

Xml

<web-app ...>

...

<context-param>

<param-name>paramName1</param-name>

<param-value>paramValue1</param-value>

</context-param>

<context-param>

<param-name>paramName2</param-name>

<param-value>paramValue2</param-value>

</context-param>

[Cui 1] </web-app>

ServletContext context = this. Getservletcontext (); [Cui 2]

String value1 = Context.getinitparameter ("paramName1");

String value2 = Context.getinitparameter ("paramName2");

[Cui 3] System. out. println (value1 + "," + value2);

Enumeration names = Context.getinitparameternames (); [Cui 4]

while (Names.hasmoreelements ()) {

System. out. println (Names.nextelement ());

}

[Cui 1] configured with two application initialization parameters

[Cui 2] get ServletContext Object

[Cui 3] get the parameter value by the parameter name

[Cui 4] get all application initialization parameter names

Learning note 4_servletcontext (sharing data between dynamic resources for the entire web app)

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.