Home > Articles

The Servlet Container

  • Print
  • + Share This
This chapter is from the book

Context-Initialization Parameters

The exam expects you to know the interface and methods to get context-initialization parameters. To understand these, remember that a Web application is a combination of JSP pages, servlets, tag libraries, JavaBeans, and other class files. The Java Virtual Machine creates a memory box for all of these called a ServletContext object that maintains information (context) about your Web application. You access the ServletContext for information about the application state. As the API states, the ServletContext grants you access to many types of information. 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 refers to a 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 the application log file. The ServletContext object enables you to set, get, and change application-level (not session-level) attributes and talk to the servlet container.

Context means application scope. The getInitParameter and getInitParameterNames methods retrieve context-wide, application-wide, or "Web application" parameters. The getInitParameter method returns a string containing the value of the parameter (you provide the name), or null if the parameter does not exist.

Some parameters have no information, so this method returns a string containing at least the servlet container name and version number. 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. Be careful; don't confuse this with session-wide attributes. The following snippet is how you might define a couple context-initialization parameters in the deployment descriptor:


Refer to Chapter 2, "Deploying Web Applications," for more information about the deployment descriptor. Presently, realize that the context-param (with subelements param-name, param-value) declares the Web application's servlet context-initialization parameters for your application. You can access these parameters in your code using the javax.servlet.ServletContext.getInitParameter() and javax.servlet.ServletContext.getInitParameterNames() methods. Remember that the name specified in the param-name element must be unique in the Web application.

Given the previous deployment descriptor snippet, you would retrieve the two pairs of parameters with the following:

    // servlet configuration initialization parameters
    Enumeration params = 
    while (params.hasMoreElements()) 
      String param = (String) params.nextElement();
      String value = 
      PrintWriter.println(param + "=" + value);
//client would receive:
  • + Share This
  • 🔖 Save To Your Account