I can see that you're confused.  This process is much easier than you're
making it.  You don't have to write any code to read or parse the
"web.xml" file.  That is done automatically by the web container, and
the servlet init parameters are automatically loaded.  Inside your
Servlet class, you can just call 'getInitParameter("<parametername>")'
to retrieve the value set in the "web.xml" file.  That's all you have to
do.

> -----Original Message-----
> From: Niketu Parekh [mailto:[EMAIL PROTECTED]
>
> Hi,
>
> I am new to JavaServlets. Learning it through the examples. I
> am trying to
> understand how servlet initialization works?
> I m using the HttpServlet.init(ServletConfig conf) method to read the
> web.xml file and set few parameters using
> ServletConfig.getInitParameter
> ("") method.
>
> As per the documentation, the web.xml file should be in \WEB-INF\
> directory. But my source code is not working. I dont get the correct
> parameter values using ServletConfig.getInitParameter(). Below is the
> source code. I would really appreciate if someone can help me
> solve this
> problem.
>
> install_dir = C:\Apache\Tomcar4.1\
> examples_dir = install_dir\webapps\ROOT\WEB-
> INF\classes\HelloWorldExample.java
> web.xml file location - install_dir\ROOT\WEB-INF\web.xml
>
> Thanks
> - Niketu
>
> SourceCode HelloWorldExample.java -
> =================================
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class HelloWorldExample extends HttpServlet {
>  private String message;
>  private String defaultmessage = "No Message";
>  private int repeats = 1;
>
>  public void init(ServletConfig config) throws ServletException {
>   super.init(config);
>   message = config.getInitParameter("message");
>   if(message == null)
>    message = defaultmessage;
>
>   try {
>    String repeatstring = config.getInitParameter
> ("repeats");
>    repeats = Integer.parseInt(repeatstring);
>   } catch (NumberFormatException ne) {
>   }
>  }
>
>  public void doGet(HttpServletRequest req, HttpServletResponse
> resp) throws ServletException, IOException {
>   resp.setContentType("text/html");
>   PrintWriter out = resp.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>");
>  }
> }
>
> SourceCode ServletUtilities.java -
> ================================
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class ServletUtilities {
>
>  public static final String docType = "<!DOCTYPE HTML PUBLIC \"-
> //W3C//DTD HTML 4.0 " + "Transitional//EN\">";
>
>  public static String headWithTitle (String title) {
>  return (docType + "\n" +
>   "<HTML>\n" +
>   "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
>  }
> }
>
> Web.xml file -
> ============
> <?xml version="1.0" encoding="ISO-8859-1"?>
>
> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
> Application2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";>
>
> <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>
> </web-app>
>
> ______________________________________________________________
> _____________
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources:
> http://java.sun.com/products/servlet/external-> resources.html
>
> LISTSERV Help:
> http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to