• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can anyone explain getServletContext method..

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to know about getServletContext method and servletConfig object??
somebody explain me please.

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the ServletContext and ServletConfig interfaces.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Arun, welcome to the Ranch!

In a java web application, a servlet has various attributes like name, initialization parameters and the context. First 2 are ok, I suppose. The context is that area of the application which can be taken as a reference to the whole application, in a way. eg: a reference in this area would be available to the whole application.

the getServletContext method of the SevletConfig interface returns the context we talked of above. A SevletConfig reference would contain the information about the servlet fetched up at the time of initialization.

Regards
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun,

They return ServletContext and servletConfig objects respectively.
ServletConfig: This is used by the container to pass information to the servlet while initializing. Information like init parameters and their values are available in this object. Info like JDBC driver name, path to database and stuffs like this can be obtained from servlet config.
In short, this gives information for a given single servlet

ServletContext: This represent the compete web application within a JVM. Typical web app containing servlets, JSPs, beans etc., run inside a single context called servlet context - a virtual wrapper. All application components can get their initialisation parameters, application attributes etc., from this object.
I short this allows application components to interact with each other and fetch application wide info from the container.

So,we can say that servletconfig is verymuch related to servlets and servletcontext is related to the container.

If anybody discovers that any of my statement/s to be incorrect, please do correct me.

Thanks everybody!

Regards,
Sriram
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Info like JDBC driver name, path to database and stuffs like this can be obtained from servlet config.



There are no direct APIs to this. But that's possible if you have access to DataSource object which might be initilized inside the servlet (through init params) or stored in somewhere you have access to.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more point-

ServletContext is an object which contains information about all servlets in a JVM so in case you are using multiple JVMs, servlet context would behave differently for different JVMs
 
reply
    Bookmark Topic Watch Topic
  • New Topic