aspose file tools*
The moose likes Servlets and the fly likes ServletContext  Vs ServletConfig Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext  Vs ServletConfig" Watch "ServletContext  Vs ServletConfig" New topic
Author

ServletContext Vs ServletConfig

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi,







When The Going Gets Tougher,The Tougher gets Going
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Please don't use code tags for plain text.

Did you look up these classes in the JavaDoc? It's pretty clear how they differ.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

i'm still unclear about the differences,hence i have posted it in the forum.
if you can illustrate the differences,it would be helpful.

--
Deepak Lal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Why don't you try to describe how you understand them to be similar, and how you understand them to be different? Explaining something can help to clarify one's thoughts, and thus one's understanding. If you go wrong, we can jump in with comments.


Ping & DNS - my free Android networking tools app
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ulff and Bear,

Below are my interpretations

ServletContext returns the current context of a web application running in a particular JVM.

getServletContext() returns the ServletContext object(at the application level)


ServletConfig is used to read the initialization parameters defined in web.xml file

getServletConfig returns the ServletConfig object.

Please correct me if the above interpretations are Incorrect and please provide me with correct differences.

I'm a bit

--
Deepak Lal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Sounds good.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

HI,

go through the links given below,

1.link1

2.link2

Hope This Helps
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thanks all for your replies.
i'm pasting the differences in case anyone wants to refer it for future
---------------
ServletConfig
---------------
One ServletConfig per servlet
it is used to pass deploy-time info to servlet and configured in the deployment descriptor file.
it is used to access ServletContext
It is within the Servlet element in Deployment descriptor.
It is accessed by using getServletConfig().getInitParameter("myname");
It is available only to the servlet in which init-param is configured.



--------------
ServletContext
---------------
It returns the current context of a web application running in a particular JVM.
if the web application is distributed,it is one per JVM.
It is used to access the <context-param> elements configured in deployment descriptor.
It is accessed by using getServletContext().getInitParameter("myname");
It is available to any servlet or jsp that is part of web application.




I hope the above interpretations can be used for later reference.



--
Deepak Lal
vijay karmani
Greenhorn

Joined: Feb 09, 2005
Posts: 4
Both ServletConfig and ServletContext are configuration objects used by servlet container to initialize various init parameters for a web application. However they differ in terms of the scope and availability of init parameters defined by both of them.

For detailed understanding, please see http://www.javashiksha.com/servlet-jsp/difference-between-servlet-config-and-servlet-context.html

Regards

Vijay Karmani
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
Hi,
my question is ServletConfig and ServletContext are interfaces implemented by GenericServlet and HttpServlet classes so how can they have objects?
Like
ServletConfig config;
ServletContext context;

if they are interface references how can they be passed as objects
example : super.init(config);

also if we use the above overriding statement
why is the following statement correct?

super.init(config);
ServletContext context=getServletContext();

what does it mean:: The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.


I have gone through the docs but couldnt understand so i am posting here. Thanks
Mohammad Monis
Greenhorn

Joined: Jan 04, 2012
Posts: 5
ServletConfig has servlet wide scope. It is defined inside <servlet> </servelet> in web.xml

ServletContext has application wide scope . It is defined outside the <servlet> tag in web.xml
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
Yes but what I would like to understand is ServletContext and ServletConfig are interfaces so how are config and context passed as objects??

http://www.java2s.com/Tutorial/Java/0400__Servlet/GetServletContextInitParameter.htm

Mohammad Monis
Greenhorn

Joined: Jan 04, 2012
Posts: 5
wea sels wrote:Hi,
my question is ServletConfig and ServletContext are interfaces implemented by GenericServlet and HttpServlet classes so how can they have objects?
Like
ServletConfig config;
ServletContext context;

[Answer1] config/ context are the objects and are type casted to ServletConfig/ ServletContext which java permits

if they are interface references how can they be passed as objects
example : super.init(config);

[Answer2] Answers1 states that config/ context are the objects so perfectly legal to pass the objects

also if we use the above overriding statement
why is the following statement correct?

super.init(config);
ServletContext context=getServletContext();

what does it mean:: The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.


I have gone through the docs but couldnt understand so i am posting here. Thanks
Mohammad Monis
Greenhorn

Joined: Jan 04, 2012
Posts: 5
wea sels wrote:Yes but what I would like to understand is ServletContext and ServletConfig are interfaces so how are config and context passed as objects??

http://www.java2s.com/Tutorial/Java/0400__Servlet/GetServletContextInitParameter.htm



objects can be type cast to the implementing interface.
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
thanks. that solved my doubts
soumyaansh roy
Greenhorn

Joined: Jan 05, 2012
Posts: 2
what are the significance of ServletContext and ServletConfig what kind of information we save in these 2 objects please give some practical examples , thanks
Mohammad Monis
Greenhorn

Joined: Jan 04, 2012
Posts: 5
soumyaansh roy wrote:what are the significance of ServletContext and ServletConfig what kind of information we save in these 2 objects please give some practical examples , thanks

ServletConfig - parameter scope is limited to the scope of the servlet in which the parameter is defined. For example, shopping cart of a user is specific to a particular user so servletconfig params can be used.
ServletContext - parameter scope is application wide and the parameter can be use to store application session info/ application initialization parameters,etc not specific to a servlet.
soumyaansh roy
Greenhorn

Joined: Jan 05, 2012
Posts: 2
Mohammad Monis wrote:
soumyaansh roy wrote:what are the significance of ServletContext and ServletConfig what kind of information we save in these 2 objects please give some practical examples , thanks

ServletConfig - parameter scope is limited to the scope of the servlet in which the parameter is defined. For example, shopping cart of a user is specific to a particular user so servletconfig params can be used.
ServletContext - parameter scope is application wide and the parameter can be use to store application session info/ application initialization parameters,etc not specific to a servlet.


hey thanks Monis , i got it !!
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Actually the servlet container will create ServletConfig object and pass it to the servlet when it's initialized. Servlet container does object creation for other interface types HTTP request, response etc.


No pain, No gain.
OCJP 1.6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Some containers even let servlets implement ServletConfig directly. getServletConfig() then simply returns this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
Deepak Lal wrote:Hi,







ServletContext: a kind of application object. Gives you the possibility to store attributes shared by all the sessions (enjoy responsibly, work thread safe)

ServletConfig: the object containing the init-param elements defined under the <servlet> tag inside the web.xml

They are different, although they sound very similar...

D.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext Vs ServletConfig