title: getServletContext NullPointerException
body: <p>I know this question has been asked a million times and there are 5 million answers - some or which are very informative.  None of which have solved this problem.  </p>
  
  <p>My goal is similar to many of the others - I want to access files - images, svgfiles, templates, etc. in the web app run-time environment.  I can make it work if I simply hard wire the directory path.  However, I would like to make my servlet portable and use a relative reference to access these resources -if for no other reason I develop in Windows and deploy in Linux.</p>
  
  <p>The basic problem is that getServletContext() is null and I am unable to determine why.</p>
  
  <p>Below is the error message, the code that produced it, followed by environment details.  Produces the same error in both development and production. I will be happy to provide any additional details if requested.</p>
  
  <p><strong>Please explain what I need to do to get this working? I will be eternally grateful.  Regards</strong></p>
  
  <pre><code>     SEVERE: Servlet.service() for servlet [jsp] in context with path [/HelloWorld] threw exception [An exception occurred processing JSP page /hello.jsp at line 19
  
       16: &lt;title&gt;Hello World&lt;/title&gt;
       17: &lt;/head&gt;
       18: &lt;body&gt;
       19:  &lt;%=wtGreet.getGreeting()%&gt; 
       20: &lt;/body&gt;
       21: &lt;/html&gt;
  
  
      Stacktrace:] with root cause
      java.lang.NullPointerException
  at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
  at HelloWorld.Greeting.getGreeting(Greeting.java:23)
  at org.apache.jsp.hello_jsp._jspService(hello_jsp.java:91)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>HelloWorld application works - produces undesired error - exactly the same as the real thing.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF 8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;jsp:useBean id="wtGreet" scope="page"
  class="HelloWorld.Greeting"&gt;
  &lt;jsp:setProperty name="wtGreet" property="who" value="World"/&gt;
  &lt;jsp:setProperty name="wtGreet" property="greet" value="Hello"/&gt;
  &lt;/jsp:useBean&gt; 
  &lt;jsp:setProperty name="wtGreet" property="*"/&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;Hello World&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;%=wtGreet.getGreeting()%&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Then Servlet that the jsp calls </p>
  
  <pre><code>package HelloWorld;
  
  import javax.servlet.http.HttpServlet;
  
  public class Greeting extends HttpServlet {
  
      private static final long serialVersionUID = 1298516959968350334L;
      private String who;
      private String greet;
  
      public void setWho(String who) {
          this.who = who;
      }
  
      public void setGreet(String greet) {
          this.greet = greet;
      }
  
      public String getGreeting() {
          System.out.println("getServletContext() == null :" + getServletContext().getContextPath());
          return "&lt;p&gt;" + this.greet + " " + this.who + "&lt;/P&gt;";
      }
  }
  </code></pre>
  
  <p><strong>Development Environment</strong>
  Windows
  Eclipse JEE
  Apache
  Tomcat 7
  JRE 7</p>
  
  <p><strong>Production Environment</strong>
  Linux
  Apache
  Tomcat 7
  JRE 8</p>