1 Reply Latest reply on May 30, 2003 6:42 AM by haraldgliebe

    could not initial my servlet

    sabar

      Hi

      pls tell me why when run my servlet, JBoss could not

      read my message in init()

      i made the following steps

      1- write my servlet

      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.
      */
      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 + "");
      }
      out.println("");
      }
      }

      2- web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
      <web-app>

      <display-name>PutHereTheNameOfYourWebApp</display-name>

      This a description of my web app made by Eclipse

      <servlet-name>ShowMessage</servlet-name>
      <display-name>This is the display name of my J2EE component</display-name>
      This is the description of my J2EE component
      <servlet-class>coreservlets.ShowMessage</servlet-class>

      <servlet-mapping>
      <servlet-name>ShowMessage</servlet-name>
      <url-pattern>/servlet/ShowMessage</url-pattern>
      </servlet-mapping>

      <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>

      <welcome-file-list>
      <welcome-file>/servlet/ShowMessage</welcome-file>
      </welcome-file-list>

      </web-app>



        • 1. Re: could not initial my servlet
          haraldgliebe

          You have to put the init param tags inside the servlet declaration, like:


          <servlet-name>ShowMessage</servlet-name>
          <servlet-class>coreservlets.ShowMessage</servlet-class>
          <init-param>
          <param-name>message</param-name>
          <param-value>Shibboleth</param-value>
          </init-param>

          ...

          Regards,

          Harald