What is the difference between ServletContext and ServletConfig?

Both are interfaces. The servlet engine implements the ServletConfig interface in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init() method.The ServletContext interface provides information to servlets regarding the environment in which they are running. It also provides standard way for servlets to write events to a log file.

Showing Answers 1 - 35 of 35 Answers

Sivakumar

  • Jun 26th, 2005
 

How can my JSP communicate with java class file.

  Was this answer useful?  Yes

Arundathi

  • Jul 1st, 2005
 

A ServletConfig object passes configuration information from the server to a servlet. ServletConfig also includes ServletContext object.  
ServletContext defines methods that allow a servlet to interact with the host server. This includes reading server-specific attributes, finding information about particular files located on the server, and writing to the server log files. I f there are several virtual servers running, each one may return a different ServletContext

  Was this answer useful?  Yes

praveen

  • Jul 22nd, 2005
 

initial parameters are stored in servletconfig where as in servletcontext global data

Veeru

  • Sep 8th, 2005
 

SevletContext 
ServletContext is represents your web application,so there is only one sevletContext for your application 
 
ServletConfig 
For particular servlet in your application there is one ServletConfig so,if 10 servlets are there in your application then there are 10 ServletConfigs

shyamalatha

  • Sep 12th, 2005
 

servlet context:it provides access to resources and that resources are common to all the servlet.servlet in an web application can use servlet context to exchange information (or) collaborate with other servlet.

servlet configuration:it provides access to initialization parameters that are configured while deploying a servlet.

Suman Sharma

  • Sep 29th, 2005
 

The init() method of a servlet uses config object to get values of parameters declared in the web.xml file.

ServletContext object binds with root-uri.

  Was this answer useful?  Yes

venkat

  • Oct 6th, 2005
 

ServletContext object is shared by all of the servlet instances within the context,whereas ServletConfig object is created for every servlet instance if we extend GenericServlet or HttpServlet.using these objects we can get env details like database driver class,url etc., if you declare these in a declarative fashion in web.xml

  Was this answer useful?  Yes

soudam srikanth

  • Mar 24th, 2006
 

when we deploy a web application the webcontainer reads the deployment descriptor(i.e,web.xml).If there is no problem then the webcontainer creates a ServletContext object for that web-application.the webcontainer removes the ServletContext objcet when the web application is stopped.so the no. of ServletContext objects present in web container depends on no.of web applications present in web.xml.for every web application a ServletContext object is created by web container.

    and WebContainer creatse ServletConfig object for every servlet that is a part of web application.As part of web application there will be only one ServletContext object,but there can be multiple ServletConfig objects...

   The webcontainer creates a ServletConfig object while creating a Servletobject.

  Was this answer useful?  Yes

the ServletConfig interface in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init () method.

The ServletContext interface provides information to servlets regarding the environment in which they are running. It also provides standard way for servlets to write events to a log file.

  Was this answer useful?  Yes

Guest

  • Mar 27th, 2007
 

ServletContext is a window given to a servlet to view its environment.It contains the global information,which are name/value pair.only one servletContext object is available per application.

ServletConfig contains information(name/value pair) specific to particular servlet used during initializing servlet. 

ServletContext object is also passed along with name/value pair,called as init param ,during application initialization.   

  Was this answer useful?  Yes

rameshvdnv

  • Apr 25th, 2007
 

Both r interfaces and suppose take a simple scenario, suppose u have a web application which is nothing but the collection of web resources suppose all  the servlets in a webapplication needs the  same resources then we must use servletcontext .suppose only for a particular servlet needs the resources then we must store that in servletconfig and we can call servletcontext object as application object.

  Was this answer useful?  Yes

ServletConfig  and ServletContext, both are the interface.
ServletConfig is used to define the header information  of a servlet . it is used to get the information of servlet instance through init() method.
ServletContext is basically a context or an enviroment in which servlet getExecute.
Through getServletContext() method we can get the context instance and we can set the attribute of the existing servlet instance. There is only one ServletContext instance.

  Was this answer useful?  Yes

We cannot differentiate ServletContext and ServletConfig, both are interface and each exist for a different purpose.

ServletContext contains the all the configuration information of the WebApplication which all the servlets in the application use. The configuration information could be connection pooling of the database access or an server startup information etc.


ServletConfig is specific to each Servlet. ServletConfig object of a Servlet consists it's the Servlet's information such as whether it is Http or a Generic Servlet or if the servlet is configured in a special way.

We can access ServletContext object from ServletConfig object.

  Was this answer useful?  Yes

ServletContext --- one per web-app, hold details which are available to entire application

ServletConfig -- one per servlet, hold information for a specific servlet.

We would have only one ServletContext for an entire web-app but could be more than one ServletConfig


Thanks,
Vinay

  Was this answer useful?  Yes

It is not necessary that the number of servlet context is one for one web-app. The number of servlet context depends on no. of JVM used in web-app for example for distributed application.

  Was this answer useful?  Yes

ServletConfig gives the information about the servlet initialization, it is an interface, where as ServletContext holds the information about whole application(project), the servlet container will create the object of ServletContext. Each servlet has its own ServletConfig object.

  Was this answer useful?  Yes

ServletContext and ServletConfig are the interfaces provided by java class in servlet API.

The vendors of web containers(WC) provides the classes implementing these interfaces.

It is the responsiblity of web container(WC) to create ServletContext and ServletConfig objects.
We can run multiple web applications as part of the WC. For there will be only every web application ONE ServletContext object and it is called as an application object.WC removes the ServletContext obj while stopping the web application.

As part of web application there can be multiple servlets.The web container is responsible for creating one ServletConfig object for every servlet.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions