So, lets get started!!!
The Servlet Context
A Web application consists of many parts. It can be a combination of JSP pages, servlets, tag libraries, Java beans, and other class files. The Java Virtual Machine creates a memory box for all of these called a ServletContext object which maintains information about our Web application. If you want to know more about your application and its state, the first place to go is the “ServletContext”. As the Servlet API states, the ServletContext allows you get many kinds of information about the Application. You can get application-level initialization parameters. You can also set and get application attributes, as well as the major and minor version of the Servlet API that this Servlet Container supports. One very interesting capability is to get hold of RequestDispatcher object to forward requests to other application components within the server, or to include responses from certain components within the servlet and to log a message to application log file. The ServletContext object is how you can set, get, and change application level attributes and talk to the Servlet Container.
Exam Trivia:
The Servlet context can be used to get, set and change application level attributes. This cannot be used to alter session parameters. Many people get confused about the ServletContext and its ability to alter the Session.
Servlet Context Methods
The getInitParameter and getInitParameterNames methods retrieve the application wide or rather the “Web application” parameters.
• The getInitParameter method returns a string containing the value of the parameter or null if the parameter does not exist.
• The getInitParameterNames method retrieves the names of the servlet's initialization parameters as an Enumeration of string objects. If there aren't any, it returns an empty Enumeration.
A Servlet that Reads the ServletContext
Let us now take a look at a simple servlet that is going to read the ServletContext and understand the initialization parameters.
Servlet Code
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletContextReadingServlet extends HttpServlet
{
/**
* Method that is going to read the Servlet Context
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("< h1 >ServletConfig " + "Initialization Parameters < / h1 >");
writer.println("< ul >");
Enumeration params = getServletConfig().getInitParameterNames();
while (params.hasMoreElements())
{
String param = (String) params.nextElement();
String value =
getServletConfig().getInitParameter(param);
writer.println("< li >< b >" + param + "< / b > = " + value);
}
writer.println("< / ul >");
writer.println("< hr >");
}
}
The above Servlet is pretty straight forward (Aren’t all of our example Servlets straight forward?). It gets all the initialization parameters as an enumeration using the getInitParameterNames() method and then just prints them all on screen, one after the other.
Previous Chapter: Chapter 10 - Form Parameters
Next Chapter: Chapter 12 - Retrieving HTTP Request Header Information

how are these parameters set? and where?
ReplyDelete@ Michee
ReplyDeleteThese parameters are set at the application level usually in the web.xml or other configuration files
Something has confused me. I've just read this in an article:
ReplyDelete"What is the difference between ServletConfig and ServletContext?
Ans: ServletConfig as the name implies provide the information about configuration of a servlet which is defined inside the web.xml file or we can say deployment descriptor.its a specific object for each servlet.
ServletContext is application specific object which is shared by all the servlet belongs to one application in one JVM .this is single object which represent our application and all the servlet access application specific data using this object.servlet also use their method to communicate with container.
"
source: http://javarevisited.blogspot.sg/2011/09/servlet-interview-questions-answers.html
What is your confusion Balazs? Am not sure what the question here is...
DeleteThe link you have given is from my friends blog and he is a very knowledgeable guy in Java.
Anand