You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Ansgar W. Konermann" <ko...@inf.tu-dresden.de> on 2001/02/23 04:00:17 UTC

GenericServlet.getServletConfig buggy?

Hi all,

I (still) have a problem with the context of a servlet.

Within a class derived from HttpServlet (nop.Receiver), this
is my doGet method:

-----
    public void doGet(HttpServletRequest rq, HttpServletResponse rs) 
        throws IOException, ServletException 
    {
        rs.setBufferSize(0);
        rs.setContentType("text/html");
        PrintWriter out = rs.getWriter();
        makeHead(out);
        out.println("<P>Chat Receiver</P>\n");
        rs.flushBuffer();

        ServletConfig myConfig = getServletConfig();
        out.println("myConfig == " + myConfig + "<br>"); // debug output
        theContext = getServletContext();
        out.println("theContext == " + theContext + "<br>"); // debug output

	// ...

-----

It gives me the following output when I call the servlet:

-----

Chat Receiver

myConfig == null

Error: 500

Location: /trial/servlet/Receiver

Internal Servlet Error:

java.lang.NullPointerException
        at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205)
        at nop.Receiver.doGet(Receiver.java:59)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
        at org.apache.tomcat.core.Handler.service(Handler.java:286)
        at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
        at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
        at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
        at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
        at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
        at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
        at java.lang.Thread.run(Thread.java:484)

-----

Please note that myConfig == null and that the following call to 
getServletContext fails with a NullPtrXcpt. From the source code
of GenericServlet I can see that getServletContext is merely
getServletConfig().getServletContext().

According to the Servlet API, Servlet.getServletConfig(),

"Implementations of this interface are responsible for storing
the ServletConfig object so that this method can return it.
The GenericServlet class, which implements this interface,
already does this."

To me, this looks like the implementation I am using is *not*
storing the ServletConfig (which was previously passed to init),
since it returns null. Therefore, GenericServlet.getServletContext
must fail (since it relies on getServletConfig).

Is this a bug or am I just too blind to see my mistake?
Any help would be greatly apreciated.

(Using Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.3.0;
Linux 2.2.16 i386; java.vendor=Sun Microsystems Inc.)

--

Best regards,

Ansgar W. Konermann

Re: GenericServlet.getServletConfig buggy?

Posted by "Ansgar W. Konermann" <ko...@inf.tu-dresden.de>.
"Craig R. McClanahan" wrote:

> but failing to call:
> 
>     super.init(config);

THX, that's been the problem.

-- 
Best regards,

Ansgar W. Konermann
eMail: ak26@inf.tu-dresden.de

Re: GenericServlet.getServletConfig buggy?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Ansgar W. Konermann" wrote:

> Hi all,
>
> I (still) have a problem with the context of a servlet.
>

99% of the time, this error is caused by your servlet having an init method:

    public void init(ServletConfig config) throws ServletException {

        ...

    }

but failing to call:

    super.init(config);

The best way to avoid this mistake is to implement the no-args version of init() instead.

Craig McClanahan