I had the same problem when I first started using init parameters. What I
found is that you can't use the ServletConfig object to retrieve parameters
from your web.xml file. You must use the ServletContext object instead, so
if you change your code to this:
message = getServletContext().getInitParameter("message");
It should work.
Thanks,
--jeff
----- Original Message -----
From: "jackling" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 14, 2001 7:15 AM
Subject: Help: init parameters fail.
> I was trying an example of java servlet book. In the example, the prog.
(see
> excerpt below) should read the parameter values from web.xml and use the
> values accordingly. However, it seems it is unable to read the values &
thus
> only able to give me default values the prog. use.
>
> I can't find what's wrong.
> advice pls.
> jackling.
>
> The ShowMessage classes is part of the mypackage package under the
mypackage
> folder. Below is the excerpt: -
>
> 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";
>
>
> 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>");
> }
>
>
> I've edited the web.xml file in the webapps\examples\web-inf directory and
> include the followings: -
> <servlet>
> <servlet-name>
> ShowMsg
> </servlet-name>
> <servlet-class>
> mypackage.ShowMessage
> </servlet-class>
>
> <init-param>
> <param-name>message</param-name>
> <param-value>Sembawang</param-value>
> </init-param>
>
> <init-param>
> <param-name>repeats</param-name>
> <param-value>5</param-value>
> </init-param>
> </servlet>
>
>