ServletConfig Interface |
public interface ServletConfig |

A ServletConfig object is used to pass parameters to a servlet during its initialization. The initialization paramters are a set of name-value pairs. The ServletConfig interface declares methods that can access the parameters as well return the name of the servlet and its associated ServletContext object. A ServletContext object contains information about the server on which the servlet resides.

Example: Reading Initialization Parameters

A servlet is initialized with a parameter read from the servlets.properties file. The parameter represents the name of a file. The ServletConfig object passed to the init()method is used to access the initialization parameter. If the parameter is read correctly, the file is opened and its contents written back to the client machine

Note that the servlet class name should not be used in the URL, but rather the name that is assigned to the servlet in the Servlet engine configuration file. The initialization parameter is associated with this name. If the servlet class name is written in the location window, the initialization parameter will not be accessed.

         import javax.servlet.*;
         import java.io.*;
         import java.util.*;
         public class InitServlet extends GenericServlet {
           String file, str;
           // The ServletConfig object passed to the init() method is
           // used to retrieve the value associated with the initialization
           // parameter "filename".
           public void init(ServletConfig config) throws ServletException {
             file = config.getInitParameter("filename");
           }
           public void service(ServletRequest request, ServletResponse response)
                   throws ServletException, IOException, FileNotFoundException {
             response.setContentType("text/html");
             PrintWriter pw = response.getWriter();
             // If the initialization parameter was successfully read, its value
             // is used to open a file.  The contents of the file are then sent
             // back to the client machine.
             if (file != null) {
               BufferedReader br = new BufferedReader(new FileReader(file));
               while ((str = br.readLine()) != null) {
                 pw.println("<B>" + str + "<BR>");
               }
             } else {
               pw.println("initialization parameter does not exist");
             }
             pw.close();
           }
         }

The web.xml file includes the following code:

                <web-app>
           ...
           <servlet>
         <servlet-name>init</servlet-name>
         <servlet-class>InitServlet</servlet-class>
             <init-param>

    <param-name>filename</param-name>

    <param-value>webpages/WEB-INF/names.txt</param-value>
         </init-param>
           </servlet>
           ...
         </web-app>

Finally, the names.txt file contains:

         Jackson's birthday is March 21
         Zachary's birthday is May 12

getServletContext() |
public ServletContext getServletContext() | Method
getServletContext() returns the ServletContext object associated with the invoking servlet. A ServletContext object contains information about the environment in which the servlet is running.

getInitParameter() |
public String getInitParameter(String name) | Method
getInitParameter() returns the value of the specified initialization parameter or null if the parameter does not exist.

getInitParameterNames() |
public Enumeration getInitParameterNames() | Method
getInitParameterNames() returns an Enumeration of String objects containing the names of all of the servlet's initialization parameters.

getServletName() |
public String getServletName() | Method
getServletName() returns the name of the servlet. If the servlet is unnamed, the method will return the servlet's class name.