What is the difference between ServletConfig
and ServletContext
interface?
问题:
回答1:
The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is used for intializing purposes.
The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. It is application scoped and thus globally accessible across the pages.
回答2:
Source : Difference between ServletConfig and ServletContext in Java
ServletConfig
ServletConfig
available injavax.servlet.*;
packageServletConfig
object is one per servlet classObject of
ServletConfig
will be created during initialization process of the servletThis Config object is public to a particular servlet only
Scope: As long as a servlet is executing,
ServletConfig
object will be available, it will be destroyed once the servlet execution is completed.We should give request explicitly, in order to create
ServletConfig
object for the first timeIn web.xml –
<init-param>
tag will be appear under<servlet-class>
tag
Here's how it looks under web.xml : (Source)
<servlet>
<servlet-name>ServletConfigTest</servlet-name>
<servlet-class>com.stackoverflow.ServletConfigTest</servlet-class>
<init-param>
<param-name>topic</param-name>
<param-value>Difference between ServletConfig and ServletContext</param-value>
</init-param>
</servlet>
ServletContext
ServletContext
available injavax.servlet.*;
packageServletContext
object is global to entire web applicationObject of
ServletContext
will be created at the time of web application deploymentScope: As long as web application is executing,
ServletContext
object will be available, and it will be destroyed once the application is removed from the server.ServletContext
object will be available even before giving the first request Inweb.xml
–<context-param>
tag will be appear under<web-app>
tag
Here's how it looks under web.xml :
<context-param>
<param-name>globalVariable</param-name>
<param-value>com.stackoverflow</param-value>
</context-param>
So finally…….
No. of web applications = That many number of ServletContext
objects [ 1 per web application ]
No. of servlet classes = That many number of ServletConfig
objects
Difference between ServletContext and ServletConfig in Servlets JSP in tabular format(Source)
回答3:
ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class.
ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container, for example, to get the MIME type of a file, to get dispatch requests, or to write to a log file. That is to get detail about its execution environment. It is applicable only within a single Java Virtual Machine. If a web application is distributed between multiple JVM this will not work. For understanding, this is like a application global variable mechanism for a single web application deployed in only one JVM.
Source: http://javapapers.com/servlet/difference-between-servletconfig-and-servletcontext/
回答4:
That's answered in the introducory text of their javadocs.
ServletConfig
javadoc:
A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
ServletContext
javadoc:
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The
ServletContext
object is contained within theServletConfig
object, which the Web server provides the servlet when the servlet is initialized.
The javadoc also contains a list of available methods along with explanation of their use. It gives a good overview of what's all available/possible with them.
回答5:
Servlet container will create two object Servlet context and Servlet Config. Servlet Context is public i.e. the data we store under Servlet context using webxml is available to all servlet and there is only one servlet context object in an application whereas in the case of servlet config which is private i.e. the data we store under Servlet context using webxml is available to particular servlet only and there can be n number of servlet config object
Example (if we have 3 servlet file in application there will be 3 servlet config object.
回答6:
ServletContext and ServletConfig Objects in the server
As part of the web server, there are many web applications that reside inside of the server. For every web application, the server is responsible for creating one object i.e servlet context object. It is also referred to as an application object.
ServletConfig Object
As part of every web-application we will create N number of servlets. For every servlet, one object will be created by the server i.e. ServletConfig object.
回答7:
ServletConfig is for a particular Servlet, and is useful in init(ServletConfig x) method, or can be used by using getServletConfig() method. It is used if we need to get some servlet specific information. For example: Servlet retriving information about database access related info might use ServletConfig as this information is required only for that particular servlet
However, ServletContext is for the whole application. It is used when we need to share information among some/all servlets/jsp's in a particular web application. It can be accessed by using getServletConfig().getServletContext() method of Servlet. For example: Servlet/JSP used for counting how many users have accessed your website.
回答8:
Difference :
.--------------------------------------------------.----------------------------------------------------------------------------------------------.
| ServletConfig | ServletContext |
:--------------------------------------------------+----------------------------------------------------------------------------------------------:
| Unique object per servlet | Unique object for complete application |
:--------------------------------------------------+----------------------------------------------------------------------------------------------:
| Used to provide init parameters to the servlet | Used to provide application level init parameters that all other servlets can use. |
:--------------------------------------------------+----------------------------------------------------------------------------------------------:
| We can’t set attributes in ServletConfig object. | We can set attributes in ServletContext that other servlets can use in their implementation. |
'--------------------------------------------------'----------------------------------------------------------------------------------------------'
回答9:
Generally both are used for initialization purpose But one main different is suppose you want to use some variable or fields for whole application you need to initialize the web.xml loading time you have one option is ServletContext
And if you want to use some variable only in particular servlet then you need to use ServletConfit.
回答10:
ServletConfig is used for sharing init parameters specific to a servlet while ServletContext is for sharing init parameters within any Servlet within a web application.
回答11:
ServletContext
interface represents Servlets view of the Web Application it belongs to.
ServletContext
present within ServeltConfig
.
Each Servlet has ServletConfig
object, used to access initParameters using getServeletConfig()
method.