Thursday, October 14, 2010

Servlet's Context and Config



For the web container is, Servlet Context Servlet interface defines a context object, this object defines a Servlet Servlet engine on the view. By using this object, Servlet can log events, obtain resources and get the engine from the Servlet class.

Servlet container is loaded at startup web applications and web applications for each Servlet context to create a unique object, could Servlet Context as a Web application server-side component of shared memory, can be stored in the Servlet Context share data, he provides four read and set the shared data. See in particular api help file.

In addition, Servlet Context object only in the web application is closed macro will only destroy, different web applications, Servlet Context independent existence.

A web application from the jsp, Servlet, javabean constitute such a collection of web components, each web application, the container will have a background object, while the javax.Servlet.Servlet Context interfaces provide a way to access the background object. You can be a Servlet examples getServlet Context () method to run them by the Servlet objects in this background, the background object from this you can access the following information resources:

1. Initialization parameters

2. Stored objects in the background

3. The resources associated with the background

4. Log

Finally, Servlet Context is my own conclusion:

Servlet Context Servlet container that is, their method can provide the same Servlet web applications were all under the use of, and for the config object, he is compared with the context, there is a lot of limitations.

When the Servlet Config object in the Servlet has been instantiated for any client to access at any time effective. But a Servlet to Servlet Config object can not be another Servlet access, that is, after a statement in the Servlet Servlet Config can only be accessed within the Servlet, are durable and effective internal variables.

Under one case, for a deep understanding of:

In general, the configuration of the entire application, in order not to use "hard coding" should be configured as Servlet Context parameter, such as character set settings.

................. CharsetGB2312 .................

Note that the above format is only 2.0 standard format after the old container (engine) with service providers own configuration format. Note that it's parent element should be that it is the role of an application.

If only a specific set of parameters Servlet to other Servlet can not be shared, it should be configured as Servlet Config parameters, such as a Servlet to read attachment to the absolute directory to use, while the other Servlet will not be used:

GetAttmail.GetAttServletabsPath / usr / mail / ax / axman / Maildir /

Needless to say, because the label has been specified name and class, this means that only mail.GetAttServlet
Servlet in order to get to the path, while others can not get to the Servlet.

Even before we talk about the attributes of this understanding, let's learn together about how to obtain the parameters of the two objects:

Access Servlet Config parameters:

First of all, to get Servlet Config object, then call it the getInitParameter (); method. To access the Servlet Config object, jsp config directly using built-in objects, but because of your JSP Servlet compiled in general will not be added to the web.xml, so generally not through jsp to take on the JSP compiled Servlet configuration parameters, then the Servlet to get Servlet Config object in two ways:

In inii () method to take to: overloaded method of passing through init

..... Public class Test extends HttpServlet (ServletConfig config; public void init (ServletConfig config) throws ServletException (this.config = config;) ..................)

Then in the following ways to access config object. But to note that in order to ensure construction methods from the config object to the current Servlet should call the parent class constructor:

..... Public class Test extends HttpServlet (ServletConfig config; public void init (ServletConfig config) throws ServletException (super.init (config); this.config = config;) ............ ......)

By getServlet Config () method directly to, the benefits of doing so is not mixing manual transmission properties, can at any time

To obtain.

A third method, it has to implement some interface, not here presented as a general discussion.

To access the Servlet Context object, as long as from the existing Servlet Config object getServlet Context () on it, and then
Call it the getInitParameter () method can access its parameters.

Ordinarily: Servlet Context Servlet Config object scope than the scope, why to get from the Servlet Config in the Servlet Context object? I personally think: container preservation for many Servlet Context object, a request which the container in the end take
To you? It contains Servlet Config take that information to you, that take Servlet Config object's parent object. As if the same HttpSession obtained from requset is taking the request object contains the current session object to you, this is only my personal idea, not had time to look at implementation. Anyway, so use it on.







Recommended links:



XviD to MP4



DOS, also use flash tips



"Nobunaga's Ambition 12 innovations," Ida 55 Owari family reunification tragic history



B2C in China's "money" King



Wizard Vertical Market Apps



Second-hand Computers Xi'an China Merchants Plaza, more than 60 illegal businesses operating wit



M2TS to MKV



PC giant brewing group prices: Three reasons support



Golden Abacus settled in Wuhan air "Gold Dealer"



reviews Audio RECORDERS



MKV to Zune



COSTIND speed up the ESTABLISHMENT of a modern shipbuilding mode guidance



sql server 2000 in data synchronization



Directory Audio CD Burners



No comments:

Post a Comment