Prepare for your Next Interview
This is a discussion on Servlet context and Servlet config within the Java forums, part of the Software Development category; What is the difference between servlet context and servlet config...
|
|||
|
Servlet Config is a private area for every servlet(variable declared here are accessible only 2 that particular servlet)
While Sevlet Context is a shared area for every servlet in an applicationi.e., all the servlet's in this application can access this variables in servlet context,an best example for this is,we can keep variables like database connection objects |
|
|||
|
Re: Servlet context and Servlet config
Servlet Config : is used to initialize any configuration value for a particular servlet engine.
generally we will configure it in the web.xml using <init-param> . we will retrieve these values in the init() method of the Servlet. For example : we can initialize the DB properties here i.e. driver name, driver url, db user and password. Servlet Context : it is used to store the java objects. these are accessed anywhere with in the Servlet Container. |
|
|||
|
Re: Servlet context and Servlet config
|
|
|||
|
Re: Servlet context and Servlet config
For every web applicaion there is one and only servletContext object. For every servlet there is a servletConfig object. All the servlets in a web application will share the data through servletContext. servletConfig is used to initialize the servlet variabes. servletContext is one of the scopes(session,request,servletContext) of the servlet to share data with other servlets.
---------web.xml----------- <servlet> <servlet-name></servlet-name> <servlet-class>Servlet1</servlet-class> <init-param> <param-name>servletconfig</param-name> <param-value>servletconfig</param-value> </init-param> <servlet> <context-param> <param-name>servletcontext</param-name> <param-value>servletcontext</param-value> </context-param> ------------------------------------------------------------------- getInitParameter("servletcofig");//only for Servlet1 getServletConfig().getServletContext().getInitParameter("servletcontext");//can be accessed at any srvlet |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| servlet - struts | psuresh1982 | Java | 4 | 08-22-2008 07:13 AM |
| when servlet get destroyed ? | palempally | Java | 3 | 10-24-2007 01:30 AM |
| servlet | nitin.pathak05 | Java | 1 | 10-24-2007 12:11 AM |
| Servlet.xml | venu.katta | Java | 1 | 10-22-2007 07:42 AM |
| How many Action Servlet will be available | Geek_Guest | Java | 0 | 07-01-2007 04:08 PM |