tags:

views:

41

answers:

1

I've added some parameters in my web.xml config file, as follow:

  <context-param>
    <param-name>service1</param-name>
    <param-value>http://www.example.com/example2.html&lt;/param-value&gt;
  </context-param>
  <context-param>
    <param-name>service2</param-name>
    <param-value>http://www.example.com/example2.html&lt;/param-value&gt;
  </context-param>
  ...

I try to get the parameter in my servlet, in particular in my service method:

protected void service(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
    System.out.println(this.getServletContext().getInitParameter("service1"));
    ...

but at runtime I have a NullPointerException... How can I get the parameter value included in web.xml?

This is the stacktrace:

GRAVE: Servlet.service() for servlet DispatcherServlet threw exception
java.lang.NullPointerException
    at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
    at it.servlethope.DispatcherServlet.service(DispatcherServlet.java:66)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

DispatcherServlet.java:66 is the line where I try the getInitParameter()

+1  A: 

If calling getServletContext() throws NPE then it means that the ServletConfig is null.

So you have overridden init(ServletConfig config) in your own servlet and you aren't properly calling super.init(config) as per the Javadoc:

Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

Fix it accordingly:

public void init(ServletConfig config) throws ServletException {
    // Do your thing here.
    // ...
    // And then finally:
    super.init(config);
}

Or just use the parameterless init() method which does not take ServletConfig so that you don't need to worry about it.

BalusC