Access to config parameters and context parameters

xiaoxiao2021-03-29  201

Although it has already answered more than once in the forum, it is now that people who truly master the difference between these two parameters are not available.

Therefore, it is necessary to write it out for you to learn.

Let's take a look at the scope of various built-in objects.

HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE: The scope of these two properties is minimal.

Time: Only the request and response is completed, of course, forward is to take the current request object to another.

A resource, in fact itself, the REQUEST object is still only survived to the end of this request, and the response is the same.

Space: You can only send a request to the client is valid.

HTTPSESSION: One connection to the client is closed, and the range of time the range is the same as the above two, the spatial appraisal range is the same.

ServletConfig: After instantification from a servlet, visit any client at any time, but only for this servlet

Effective, a servletconfig object of a servlet cannot be accessed by another servlet.

ServletContext: Anyone is valid at any time, which is a true global object.

So how should servletConfig parameters and servletContext parameters should be used, how to get?

In general, the configuration of the entire application, in order not to use "hard coding", it should be configured as a servletContext parameter, such as words.

The symbol set setting.

.................

charset

GB2312

.................

Note that the above format is only 2.0 standard formats, and the old container (engine) is configured with the service provider's own format. Pay attention to it

The parent element should be that is to say it is for an application.

And if there is only one specific servlet to set the parameters, other servlets cannot be shared, and should be configured as servletconfig

Parameters, such as a servlet that reads attachments to use absolute directories, and other servlets will not be used:

getatt

mail.getattrServlet

Abspath

/ usr / mail / ax / axman / maildir /

Needless to say, because Name and Class have been specified in the tag, that is, only mail.getattrservlets

You can take the path in the servlet, and other servlets cannot be taken.

So how do you access the parameters of these two objects?

Access ServletConfig parameters:

First get a servletconfig object, then call its GetItParameter (); method. Have access to

ServletConfig objects, using config built-in objects directly in JSP, but because of your JSP compile, servlet will not be

Add to Web.xml, so generally do not take the configuration parameters for servlet compiled this JSP through JSP, then there are two ways to get the servletconfig object:

Take it in the inIi () method: transfer through the init

.....

Public Class Test Extends Httpservlet

{

ServletConfig Config;

Public void init (servletconfig config) throws servletexception {

THIS.CONFIG = Config;

}

..................

}

The CONFIG object can then be accessed in the following method. But note that in order to ensure that it can go to the current servlet from the construction method.

Config object, the method of constructing the parent class should be called:

.....

Public Class Test Extends Httpservlet

{

ServletConfig Config;

Public void init (servletconfig config) throws servletexception {

Super.init (config);

THIS.CONFIG = Config;

}

..................

}

The benefits of doing this through the GetServletConfig () method, do not have to transfer the attributes, want to be at any time

Get it.

There is also the third method, to implement some interfaces yourself, here is not introduced as a general discussion.

To access the ServletContext object, just getServletContext () from the existing servletconfig object, then

Calling it GetItParameter () method can get its parameters.

According to the: servletContext object's scope is larger than the servletconfig role domain, why should you get from servletconfig?

ServletContext object? I personally think that the container saves a lot of servletContext objects, which one is taken when the container is taken.

Give you? Then take the one containing the servletconfig information, just say the parent object of the ServletConfig object. Just

Like httpsession to get from the REQUSET, take the session object that contains the current REQUESE object to you, this is just me.

Individual idea, have not yet come and see specific implementation. Anyway, let's use it.

转载请注明原文地址:https://www.9cbs.com/read-130467.html

New Post(0)