You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2000/11/01 02:15:43 UTC

Re: uninitialized instance variables in servlet

Have you checked your servlet logs to see if the UnavailableException errors
actually are being thrown?  You don't say what version of Tomcat you are using,
but (if I remember correctly) version 3.1 did not actually honor
UnavailableExceptions thrown from init().

Craig McClanahan


Tom O'Connor wrote:

> In the init() method of my Servlet class, I populate 2 String instance
> variables that are later used in the doPost() method.  The String values are
> populated by calling .getInitParameter() on the ServletConfig instance
> passed into init().  I verify that the String returned from
> .getInitParameter() != null inside init().
>
> After tomcat starts, I start a client program that generates HTTP requests
> aimed at the servlet.  More often than not, these requests fail with 500
> Server Error as the response.  For each failed request, in
> $TOMCAT_HOME/logs/tomcat.log there is a
> NullPointerException and a stack trace, and the stack trace points me to a
> line in my servlet where I invoke a method on the String instance variables
> that should have been initialized in init().
>
> This is *NOT* a configuration file issue.  I can say this because in the
> same webapp, I have 2 servlets that each have the same servlet-class element
> in web.xml.  The first servlet will generate failures (sometimes) but the
> second servlet (which is the same class, but a different instance) will
> succeed.  The only thing different between the two instances are the
> init-param values in web.xml.  I also swap which servlet I invoke first and
> it's the first servlet invoked that fails.
>
> I've tried this using both Apache+mod_jk over Ajp13, and invoking the
> servlet directly through Tomcat's HTTP adapter.
>
> Doesn't the servlet spec say that no thread shall enter Servlet.service()
> until Servlet.init() has successfully completed?  If that's true, how can my
> instance variables be null?
>
> The servlet looks like this:
>
> public class S extends HttpServlet {
>    private String n = null;
>    private String v = null;
>    public void init(ServletConfig config) throws ... {
>       super.init(config);
>       n = config.getInitParameter("n");
>       v = config.getInitParameter("v");
>       if (n == null) throw new UnavailableException();
>       if (v == null) throw new UnavailableException();
>         // snip
>    }
>    public void doPost(...) throws ... {
>       // this.n and this.v are used in here
>    }
> }