You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Will Hartung <wi...@msoft.com> on 2002/07/18 19:01:21 UTC

Portabilty concern for getServletContext (was Re: ??? Tomcat Bug? -- Throws NullPointerException ???)

From: "Tony LaPaso" <to...@absolutejava.com>
Sent: Wednesday, July 17, 2002 5:53 PM
Subject: ??? Tomcat Bug? -- Throws NullPointerException ???
> 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

I just wanted to point something out to folks out there using JSPs.

We are encountering this right now, and this sort of brings it to light.

In Tomcat 3.x (Not sure about TC 4), you have access to and can use
"getServletContext" within a JSP. No doubt this is because the actual JSP
servlet is descendant, or at least mimics the capabilities of
GenericServlet, which has "getServletContext" as a convenience method.

However, a key point to realize is that the Servlet INTERFACE (which is what
Servlets are required to implement), does NOT offer "getServletContext" as a
convenience method. So, using "getServletContext" from within a JSP is NOT
portable. This is a subtle thing.

Specifically, it is not portable to Weblogic.

Just a little FYI so you do not have to go back through all of the JSPs when
it comes time to port. It is quite a pain.


Something to be conscious of if you are trying to right portable code.

Regards,

Will Hartung
(willh@msoft.com)




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>