File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes ServletContext returns null on Tomcat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "ServletContext returns null on Tomcat?" Watch "ServletContext returns null on Tomcat?" New topic

ServletContext returns null on Tomcat?

Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75

The basics... Apache 1.3x coupled to Tomcat 4.1 using mod_jk. Apache's httpd.conf is set up to send *.jsp and /servlet/* to tomcat. Tomcat has the context path set to "" (the default) and the doc path set to "public_html (WEB-INF is under public_html). Servlets are mapped "/servlet/servletname" Jsp's work fine, and so do simple servlets (both mapped, and using the invoker statement).

My problem is when I try to use a RequestDispatcher (both ServletContext, and HttpRequest). I've managed to track it down to the fact that Tomcat is returning null when I getServletContext(). It's not the code because it works on Resin.

My guess is it's either a Context path issue in server.xml, and/or a servlet mapping problem. I've tried setting the context path to "/" and that gives me a 500 error on tomcat. I've tried moving the webapp to a different dir and setting a new context path and that gives me a 404 error (using both mapping and invoker).

Anyone have any clues what's up?

Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
After much hair pulling, I managed to solve the problem, but I have no idea why... Maybe someone can explain it to me....

On both Tomcat 4.0 and Resin, getServletContext() gave me a servlet context. On Tomcat 4.1, I had to use getServletConfig().getServletContext().

Anyone ever hear of this before, or have any ideas?

I agree. Here's the link:
subject: ServletContext returns null on Tomcat?
It's not a secret anymore!