Servlet Menu


Reading Initialization Paraeters in Servlet




Both ServletContext and ServletConfig are basically the configuration objects which are used by the servlet container to initialize the various parameter of a web application. But they have some difference in terms of scope and availability.

Difference between ServletConfig vs. ServletContext

ServletConfig

ServletContext

ServletConfig object is one per servlet class. ServletContext object is global to the entire web application.
Object of ServletConfig will be created during the initialization process of the servlet. Object of ServletContext will be created at the time of web application deployment
We have to give the request explicitly in order to create the ServletConfig object for the first time. ServletContext object can be available even before giving the first request.
Scope: As long as a servlet is executing, the ServletConfig object will be available, it will be destroyed once the servlet execution is completed. Scope: As long as a web application is executing, the ServletContext object will be available, and it will be destroyed once the application is removed from the server.
ServletConfig object is used while only one servlet requires information shared by it. ServletContext object is used while application requires information shared by it.
getServletConfig() method is used to obtain Servletconfig object. getServletContext() method is used to obtain ServletContext object.
In web.xml — tag will be appear under tag. In web.xml — tag will be appear under tag.

ServletConfig

An object of ServletConfig is created by the web container for each servlet. This object can be used to get configuration information from web.xml file. If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time.

Methods of ServletConfig interface

public String getInitParameter(String name): Returns the parameter value for the specified parameter name.
public Enumeration getInitParameterNames(): Returns an enumeration of all the initialization parameter names.
public String getServletName(): Returns the name of the servlet

web.xml
<web-app>
	<servlet>
		<servlet-name>Config</servlet-name>
		<servlet-class>ConfigDemo</servlet-class>
		<init-param>  
			<param-name>username</param-name>  
			<param-value>studyglance</param-value>  
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Config</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

ConfigDemo.java
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class ConfigDemo extends HttpServlet {

   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();

	  //get ServletConfig object.
	  ServletConfig config=getInitServletConfig();

	  //get init parameter from ServletConfig object.
      String name= config.getParameter("username");
	  out.println("Welcome "+name);
	  out.close()
   }
}

ServletContext

ServletContext object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application. If any information is shared to many servlet, it is better to provide it from the web.xml file using the element.

Methods commonly used in ServletContext interface

public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters.
public void setAttribute(String name,Object object):sets the given object in the application scope.
public Object getAttribute(String name):Returns the attribute for the specified name.

web.xml
<web-app>
	<context-param>  
		<param-name>driver</param-name>  
		<param-value>com.mysql.jdbc.Driver</param-value>  
	</context-param>
	<servlet>
		<servlet-name>Context</servlet-name>
		<servlet-class>ContextDemo</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Context</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

ConfigDemo.java
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class ContextDemo extends HttpServlet {

   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();

	  //get ServletContext object.
	  ServletContext context=getServletContext();

	  //get init parameter from ServletConfig object.
      String driverName= context.getInitParameter("driver");
	  out.println("driver name is = "+driverName); 
	  out.close()
   }
}


Next Topic :Session Tracking

Suggestion/Feedback Form :




Excellent  Very Good  Good  Average  Poor


This is a Compliment
This is a Suggestion for improvement
This is Feedback
This is Grievance