tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alberto Avritzer <b...@spiff.hr.att.com>
Subject getting init parameters from web.xml (tomcat 3.1)
Date Tue, 08 Aug 2000 18:42:30 GMT
Hi there! I am
going through a basic
book on servlets and
I was not able to get
the following code to work
on tomcat 3.1. I keep
geting the default message
output even when I set
the web.xml content as follows,
which should produce 5 repeats
of Sbhiboleth.
So I have two questions:
1. Where is the proper place in
the tomcat 3.1 directory structure
to put the web.xml file.
2. Is there a bug in the code
below that is preventing me
from getting the correct result?

Thanks,

-Beto

  <?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE web-app (View Source for full doctype...)>
- <web-app>
- <servlet>
  <servlet-name>ShowMsg</servlet-name>
  <servlet-class>coreservlets.ShowMessage</servlet-class>
- <init-param>
  <param-name>message</param-name>
  <param-value>Shibboleth</param-value>
  </init-param>
- <init-param>
  <param-name>repeats</param-name>
  <param-value>5</param-value>
  </init-param>
  </servlet>
- <taglib>
  <taglib-uri>/tags</taglib-uri>
  <taglib-location>/WEB-INF/tags/HelloTagLib.tld</taglib-location>
  </taglib>
  </web-app>

package coreservlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Example using servlet initialization. Here, the message
 *  to print and the number of times the message should be
 *  repeated is taken from the init parameters.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2000 Marty Hall; may be freely used or adapted.
 */

public class ShowMessage extends HttpServlet {
  private String message;
  private String defaultMessage = "No message.";
  private int repeats = 1;

  public void init(ServletConfig config)
      throws ServletException {
    // Always call super.init
    super.init(config);
    message = config.getInitParameter("message");
    if (message == null) {
      message = defaultMessage;
    }
    try {
      String repeatString = config.getInitParameter("repeats");
      repeats = Integer.parseInt(repeatString);
    } catch(NumberFormatException nfe) {
      // NumberFormatException handles case where repeatString
      // is null *and* case where it is something in an
      // illegal format. Either way, do nothing in catch,
      // as the previous value (1) for the repeats field will
      // remain valid because the Integer.parseInt throws
      // the exception *before* the value gets assigned
      // to repeats.
    }
  }

    public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
        throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String title = "The ShowMessage Servlet";
      out.println(ServletUtilities.headWithTitle(title) +
                  "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                  "<H1 ALIGN=CENTER>" + title + "</H1>");
      for(int i=0; i<repeats; i++) {
        out.println(message + "<BR>");
      }
      out.println("</BODY></HTML>");
    }
}



Mime
View raw message