Request.getContextPath() throws nullpointer Exception in Linux
Recently I faced this problem. I have tomcat deployed both on windows server and one of my Linux server where I use windows server for development and Linux for production.
My code uses the HttpRequest’s request.getContextPath() function in some of the servlets. In Linux server the application war is placed in /home/smitha/myWork/ directory (not in the webapp folder of tomcat) and specified it in the server.xml file:
<Context path=”/myApp” docBase=”/home/smitha/myWork/myApp” debug=”5″ reloadable=”true” crossContext=”true” />
In windows everything worked fine. But on Linux machine, my application initially loaded fine ( showed index page) but request.getContextPath() threw ‘NullPointerExcpetion’.
Also in logs I saw an Exception saying the directory =”/home/smitha/myWork/myApp doesn’t exist.
The application war file has to be unzipped by tomcat if the application is deployed properly.. but it was not happening. The cause for this problem was the directory =”/home/smitha/myWork/ was not owned by tomcat user. Tomcat user need to have permissions to create directory in the myWork directory to unzip the war.
By changing the ownership of the directory using chown command I solved the problem. Alternatevely one can also add the owner of /home/smitha/myWork/ directory to tomcat group and grant write permission to the group. This will also solve the problem.

