ServletContext:
- You have only one ServletContext per web application
- Your Deployment Descriptor (web.xml) looks like the following when you specify context init parameters.
<web-app ……>
<context-param>
<param-name>myName</param-name>
<param-value>sukumar vaddi</param-value>
<context-parm>
</web-app>
- 3. You read the context parameters in the servlet using the code getServletContext().getInitParameter(“myName”);
- This is available to all the servlets and JSPs that make up the web application.
ServletConfig:
- You have one servletConfig per servlet.
- Your Deployment Descriptor (web.xml) looks like the following when you specify servlet init parameters.
<web-app ……>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>myDomain.ServletInit</servlet-class>
</servlet>
<int-param>
<param-name>myName</param-name>
<param-value>sukumar vaddi</param-value>
<init-parm>
</web-app>
- 3. You read the servlet init parameters in the servlet using the code getServletConfig().getInitParameter(“myName”);
- 4. ServletConfig is available to only the servlet for which <init-param> was defined. But it can be made widely avialbe by storing it as an attribute.
Source: Head First Servlets and JSP
Advertisements