Just because you are creating an inner class within a servlet does not mean 
that that inner class now gets access to the ServletConfig.   That is what 
is happening when you call getServletContext() from the init() method.  You 
are implicitly saying config.getServletContext (where config is the current 
ServletConfig object).

If you pass in the ServletConfig object, you will be able to use it and not 
go "boom"

  private void readObject(ObjectInputStream ois, ServletConfig config) .....

call that from the init() by doing:

Inner inner = (Inner)ois.readObject(this);

BTW, I don't understand how you have the method defined as:
  private void readObject(ObjectInputStream ois)
but then you access it like:
Inner inner = (Inner)ois.readObject();

readObject() doesn't exist, but readObject(ObjectInputStream) does.

I'm probably just missing something.

Anyway, you have to pass the implicit "this" config object in there or you 
won't have access to the ServletConfig object.


Jake

At 07:53 PM 7/17/2002 -0500, you wrote:
>Hello,
>
>I was hoping somebody could run this on a non-Tomcat server. I've
>used TC v4.0.4 and v4.1.7 and both result in NPEs being thrown
>when I call getServletContext() from within the readObject()
>method of an inner class. Below is a very simple servlet that
>shows the problem.
>
>As you can see, the init() method is able to call
>getServletContext() with no problem. BUT, when init() tries to
>de-serialize an object which is an inner class, the object's
>readObject() method cannot call getServletContext().
>
>I'm not looking for "work-arounds" -- I just want to know if this
>is a Tomcat bug.
>
>
>
>
>
>
>import java.io.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>
>public class HelloWorld extends HttpServlet {
>    public void doGet(HttpServletRequest request,
>                      HttpServletResponse response)
>                      throws ServletException, IOException {
>       response.setContentType("text/html");
>       PrintWriter out = response.getWriter();
>       out.write("<html><head><title>Hello</title></head><body>");
>       out.println("Hello there....</body</html>");
>    } // doGet()
>
>    private class Inner implements Serializable {
>       private Inner() {}
>
>       private void readObject(ObjectInputStream ois)
>                               throws Exception {
>          System.out.println("In Inner.readObject()...");
>          ois.defaultReadObject();
>          System.out.println("defaultReadObject completed...");
>          ServletContext sc = getServletContext();  // BOOM!!
>          System.out.println("Got the servlet context!!!");
>       } // readObject()
>    } // Inner
>
>    public void init() {
>       try {
>          System.out.println("The servlet context is: " +
>getServletContext());
>          ByteArrayOutputStream baos = new
>ByteArrayOutputStream();
>          ObjectOutputStream oos = new ObjectOutputStream(baos);
>          oos.writeObject(new Inner());
>          oos.close();
>
>          // Now, deserialize it....
>          ObjectInputStream ois = new ObjectInputStream(new
>ByteArrayInputStream(baos.toByteArray()));
>          Inner inner = (Inner)ois.readObject();
>          System.out.println("Got the inner object...");
>       } catch(IOException e) {
>          System.err.println("Got an exception in init()..." + e);
>          e.printStackTrace();
>       } catch(ClassNotFoundException e) {
>          System.err.println("Got an exception in init()..." + e);
>          e.printStackTrace();
>       }
>    } // init()
>} // HelloWorld
>
>
>
>--
>To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to