• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ServletContext Vs ServletConfig

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,





 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds good.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

go through the links given below,

1.link1

2.link2

Hope This Helps
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. that solved my doubts
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some containers even let servlets implement ServletConfig directly. getServletConfig() then simply returns this.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic