On Mon, 14 May 2001, Jeff Kilbride wrote:
> 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.
A couple of things:
ServletConfig.getInitParameter() should be the same as
GenericServlet.getInitParameter() (the latter is a convenience method
for the former). It gets servlet initial parameters, as specified by
init-param's in web.xml.
ServletContext.getInitParameter() gets context initial parameters, as
specified by context-param's in web.xml.
So they are different things.
(See http://java.sun.com/products/servlet/2.2/javadoc/index.html.)
See Bo Xu's comments about named/defined servlets (in web.xml), and
what URL you are using to call a servlet. That has a great impact on
whether/how initial parameters (both servlet and context) work.
> ----- 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>
> >
> >
>
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]