4.1 The Servlet

Java Servlet Programming, 2nd Edition > 4. Retrieving Information > 4.1 The Servlet

 
< BACKCONTINUE >

4.1 The Servlet

Each registered servlet name can have specific initialization (init) parameters associated with it. Init parameters are available to the servlet at any time; they are set in the web.xml deployment descriptor and generally used in init( ) to set initial or default values for a servlet or to customize the servlet's behavior in some way. Init parameters are more fully explained in Chapter 3.

4.1.1 Getting a Servlet Init Parameter

A servlet uses the getInitParameter( ) method for access to its init parameters:

public String ServletConfig.getInitParameter(String name)

This method returns the value of the named init parameter or null if it does not exist. The return value is always a single String. It is up to the servlet to interpret the value.

The GenericServlet class implements the ServletConfig interface and thus provides direct access to the getInitParameter( ) method. This means the method can be called like this:

public void init() throws ServletException {   String greeting = getInitParameter("greeting"); }

A servlet that needs to establish a connection to a database can use its init parameters to define the details of the connection. We can assume a custom establishConnection( ) method to abstract away the details of JDBC, as shown in Example 4-1.

Example 4-1. Using init Parameters to Establish a Database Connection
java.sql.Connection con = null; public void init() throws ServletException {   String host = getInitParameter("host");   int port = Integer.parseInt(getInitParameter("port"));   String db = getInitParameter("db");   String user = getInitParameter("user");   String password = getInitParameter("password");   String proxy = getInitParameter("proxy");   con = establishConnection(host, port, db, user, password, proxy); }

There's also another more advanced and standard abstraction model available to servlets designed for Java 2, Enterprise Edition (J2EE). See Chapter 12.

4.1.2 Getting Servlet Init Parameter Names

A servlet can examine all its init parameters using getInitParameterNames( ):

public Enumeration ServletConfig.getInitParameterNames()

This method returns the names of all the servlet's init parameters as an Enumeration of String objects or an empty Enumeration if no parameters exist. It's most often used for debugging.

The GenericServlet class additionally makes this method directly available to servlets. Example 4-2 shows a servlet that prints the name and value for all of its init parameters.

Example 4-2. Getting init Parameter Names
import java.io.*; import java.util.*; import javax.servlet.*; public class InitSnoop extends GenericServlet {   // No init() method needed   public void service(ServletRequest req, ServletResponse res)                              throws ServletException, IOException {     res.setContentType("text/plain");     PrintWriter out = res.getWriter();     out.println("Init Parameters:");     Enumeration enum = getInitParameterNames();     while (enum.hasMoreElements()) {       String name = (String) enum.nextElement();       out.println(name + ": " + getInitParameter(name));     }   } }

Notice that this servlet directly subclasses GenericServlet, showing that init parameters are available to servlets that aren't HTTP servlets. A generic servlet can be used in a web server even though it lacks any support for HTTP-specific functionality.

4.1.3 Getting a Servlet's Name

Also in the ServletConfig interface there's a method that returns the servlet's registered name:

public String ServletConfig.getServletName()

If the servlet is unregistered, the method returns the servlet's class name. This method proves useful when writing to logs and when storing a servlet instance's state information into a shared resource such as a database or the servlet's SessionContext that we'll learn about shortly.

As an example, the following code demonstrates how to use the servlet's name to retrieve a value from the servlet context, using the name as part of the lookup key:

public void doGet(HttpServletRequest req, HttpServletResponse res)                        throws ServletException, IOException {    String name = getServletName();   ServletContext context = getServletContext();    Object value = context.getAttribute(name + ".state");  }

Using the servlet name in the key, each servlet instance can easily keep a separate attribute value within the shared context.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net