Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone explain getServletContext method..

 
ArunKumar Sivasamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know about getServletContext method and servletConfig object??
somebody explain me please.

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the ServletContext and ServletConfig interfaces.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • 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
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • 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: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Pankaj Upadhyay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic