Differences Between ServletContext and ServletConfig


ServletContext:

  1. You have only one ServletContext per web application
  2. 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>

  1. 3. You read the context parameters in the servlet using the code  getServletContext().getInitParameter(“myName”);
  2. This is available to all the servlets and JSPs that make up the web application.

ServletConfig:

  1. You have one servletConfig per servlet.
  2. 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>

  1. 3. You read the servlet init parameters in the servlet using the code getServletConfig().getInitParameter(“myName”);
  2. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s