Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes can anyone explain getServletContext method.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "can anyone explain getServletContext method.." Watch "can anyone explain getServletContext method.." New topic
Author

can anyone explain getServletContext method..

ArunKumar Sivasamy
Greenhorn

Joined: Jul 23, 2009
Posts: 2
I need to know about getServletContext method and servletConfig object??
somebody explain me please.

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Check the ServletContext and ServletConfig interfaces.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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

Experience and talent are independent of age
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
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

Joined: Mar 24, 2008
Posts: 3855

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.
Pankaj Upadhyay
Greenhorn

Joined: Nov 06, 2008
Posts: 21
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


Pankaj Upadhyay (SCJP 1.6 == 86%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone explain getServletContext method..