Hello And Happy Friday to all.
I am working out of Marty's Core Servlets book and I am having trouble
getting an init statement to work.
For those that have the book it is on page 40. If you don't have the book, I
will try and explain it here.
The code below is suposed to look at the web.xml file for some init
parameters. According to the book the web.xml file is to be placed in the
/installdir/webpages/WEB-INF and the file just lists param names and values.
And according to the book, when a servlet gets called and it has the
following method
public void init(ServletConfig config)
super.init(config);
message = config.getInitParameter(message);
that this init gets called just once, and all subsequent hits of this are
threaded. So its a one time initialization.
I have the web.xml file filled out properly and I have my class file in the
/installdir/webapps/root/web-inf/classes/coreservlets and I marked my code
with package coreservlets. All works, it just does not go out and get the
web.xml file for my info.
Below is all the code, you can try and run it, if you like. It looks kind of
cool, and I would like to get it to run.
If anyone understands, or knows of a missing config in my setup, please let
me know.
Thanks
Scott Purcell
code #
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowMessage extends HttpServlet {
private String message;
private String defaultMessage = No New Messages;
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 nfe) {
}
}
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(ServletUtil.headWithTitle(title) +
BODY BGCOLOR=\#fdf5e6\\n +
H1 ALIGN=CENTER + title + /h1);
for (int i=0; irepeats; i++) {
out.println(message + br);
}
out.println(/BODY/HTML);
}
}
xml
?xml version=1.0 encoding=ISO-8859-1?
!DOCTYPE web-app
PUBLIC -//Sun Microsystems, Inc.//DTD Web Application 2.2//EN
http://java.sun.com/j2ee/dtds/web-app_2.2.dtd;
web-app
servlet
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
init-param
param-name
repeats
/param-name
param-value
10
/param-value
/init-param
/servlet
/web-app