I have Tomcat 4.0 running as a service on Windows 2000 platform. I am trying to get a
servlet to work with init parameters.
This is the code:
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*Example using servlet initialization.
**/
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("repeat");
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
Intiger.pareseInt 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>");
}
}
ant this is the web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<servlet>
<servlet-name>
ShowMsg
</servlet-name>
<servlet-class>
coreservlets.ShowMessage
</servlet-class>
<init-param>
<param-name>
message
</param-name>
<param-value>
HelloWorld
</param-value>
</init-param>
<init-param>
<param-name>
repeats
</param-name>
<param-value>
10
</param-value>
</init-param>
</servlet>
</web-app>
I am suppose to see Hello World repeated 10 times, but I don't so for some reason it
is not retrieving the init paramets in the web.xml file? Please can anyone help me
with this. Remember I am very new to this. If there is some other page you can
re-direct me too I would appreciate it.