-
Junior Member
Servlet context and Servlet config
What is the difference between servlet context and servlet config
-
Junior Member
Re: 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
-
Junior Member
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.
-
Junior Member
Re: Servlet context and Servlet config
-
Junior Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules