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]>