开发者

ServletConfig vs ServletContext

开发者 https://www.devze.com 2023-01-26 02:06 出处:网络
What is the difference between ServletConfig and ServletContext开发者_开发百科 interface?The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is us

What is the difference between ServletConfig and ServletContext开发者_开发百科 interface?


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.


Source : Difference between ServletConfig and ServletContext in Java

ServletConfig

  • ServletConfig available in javax.servlet.*; package

  • ServletConfig object is one per servlet class

  • Object of ServletConfig will be created during initialization process of the servlet

  • This 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 time

  • In 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 in javax.servlet.*; package

  • ServletContext object is global to entire web application

  • Object of ServletContext will be created at the time of web application deployment

  • Scope: 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 In web.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)

ServletConfig vs ServletContext


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/


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 the ServletConfig 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.


ServletConfig vs ServletContext

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. |
'--------------------------------------------------'----------------------------------------------------------------------------------------------'


ServletConfig vs ServletContext

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.


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消