About servletconfig parameters and servletContext parameters
Although I have already answered more than once in the forum, it is now that people who really master these two parameters are not available. So it is necessary to write it out for everyone to learn. Let's take a look at various built-in The scope of object
● HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE: These two attributes are minimized.
Time: Just the request and the response completion will be invalid, of course, the forwarding is to pass the current Request object to another resource. In fact, the Request object itself is only survived to the end of this request, and Response is also.
Space: You can only send a request to the client is valid.
● httpsession: One connection to the client is closed, the time scope is the same as two of the above, the range of space is the same.
● ServletConfig: After being instantiated from a servlet, access to any client at any time is valid, but only the servlet is valid, a servletsConfig object cannot be accessed by another servlet.
● ServletContext: Any servlet, anyone is valid at any time, this is the real 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 character set settings.
.................
init-param>
.................
web-app>
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. Note that its parent element should be
And if there is only one specific servlet to set the parameters, other servlets cannot be shared, and should be configured as a servletconfig parameter, such as a servlet that reads attachments to use absolute directories, and other servlets will not be used:
init-param>
servlet>
Needless to say, because Name and Class have been specified in the
So how do you access the parameters of these two objects?
First, the method of accessing the parameters of the ServletConfig object:
To access the parameters of the ServletConfig object, first get the servletconfig object, then call its getInitParameter (); method. To access the ServletConfig object, use the config built-in object directly in the JSP, but because of your JSP compile, servlet will generally not be added to Web.xml, so it generally does not take the JSP compiled servlet compiled servlet. parameter. There are two ways to get a servletconfig object in the servlet: 1. 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;
}
..................
}
2, through the getServletConfig () method directly, the advantage is that it is not necessary to transfer the attribute, and I want to get at any time.
There is also the third method, to implement some interfaces yourself, here is not introduced as a general discussion.
Second, the method of accessing the parameters of the servletContext object:
To access the parameters of the ServletContext object, first get the servletContext object, then call the GetItParameter () method of this object to get its parameters. However, to get a servletContext object, just getServletContext () from the existing servletconfig object.
Thinking:
According to the: servletContext object's scope is larger than the servletconfig scope, why should I get a servletContext object from servletconfig? I personally think that the container saves a lot of servletContext objects. Which one is to you when the container is requested? Then take the one containing the servletconfig information, just say the parent object of the ServletConfig object. Just as httpsession wants to make the same from the Requset, take the session object that contains the current REQUESE object to you, this is just my personal thoughts, there is no time to see the specific implementation. Anyway, let's use it.