Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServletConfig vs ServletContext

What is the difference between ServletConfig and ServletContext interface?

like image 230
Sumithra Avatar asked Nov 19 '10 09:11

Sumithra


People also ask

What is the ServletConfig object and and ServletContext?

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.

What is ServletContext and ServletConfig in spring?

ServletConfig is used by only single servlet to get configuration information, whereas ServletContext is used by multiple objects to get configuration information. ServletConfig object is one per servlet class and destroyed once the servlet execution is completed.

What is ServletConfig?

ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization. ServletConfig is for a particular servlet, which means one should store servlet-specific information in web. xml and retrieve them using this object.

What is ServletContext object?

The object of ServletContext provides an interface between the container and servlet. The ServletContext object can be used to get configuration information from the web. xml file. The ServletContext object can be used to set, get or remove attribute from the web.


2 Answers

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.

like image 191
lucas1000001 Avatar answered Oct 15 '22 01:10

lucas1000001


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)

enter image description here

like image 44
KNU Avatar answered Oct 14 '22 23:10

KNU