Using INIT Parameters on ServletConfig-Wants Some Help
843840Oct 10 2002 — edited Oct 14 2002Tomcat 4.0.4 Issues:
I am new to servlet and I encountered a small problems using Initialization Parameters. I seems that the program doesnt pick the defined values of parms. Any help will be greatly appreciated. Here is the code snippet ..
The path is shown here.
MyDevtDir/webapps/examples/web-inf/classes/coreservlets - copied at coreservlets
by Martin Hall
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletPgm1 extends HttpServlet {
private String name;
private String defaultName = "No name listed.";
private int counter = 1;
public void init(ServletConfig config)
throws ServletException {
// Always call super.init
super.init(config);
name = config.getInitParameter("name");
if (name == null) {
name = defaultName;
}
try {
String repeatString = config.getInitParameter("counter");
counter = Integer.parseInt(repeatString);
} catch(NumberFormatException nfe) {
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "The ShowName 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>");
}
}
The path is shown here.
MyDevtDir/webapps/examples/web-inf/web.xml - copied at coreservlets
by Martin Hall
<?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>
ShowName
</servlet-name>
<servlet-class>
coreservlets.ServletPgm1
</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>Skywalker</param-value>
</init-param>
<init-param>
<param-name>counter</param-name>
<param-value>10</param-value>
</init-param>
</servlet>
</web-app>