在Java中更改标准servlet构造函数

时间:2014-04-02 22:50:08

标签: java servlets web java-server

我有以下代码:

public BalleServlet() throws SAXException, IOException, ParserConfigurationException, ServletException {
    super();
    ServletContext context = getServletContext();
    InputSource xmlFile = new InputSource(context.getResourceAsStream("/WEB-INF/balle.xml"));
    xd = new XMLDoc(xmlFile);
    // TODO Auto-generated constructor stub
}

我曾经只是

public BalleServlet() throws SAXException, IOException, ParserConfigurationException, ServletException {
    super();
}

这完美无缺。

问题是getServletContext()似乎抛出了NullPointerException,我无法完全理解它为什么这样做。我看到很多其他人也对此感到愤怒,但我还没有能够使用所提供的解决方案。这是堆栈跟踪btw:

java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
at Martin.Berntsen.BalleServlet.<init>(BalleServlet.java:36)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:310)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:138)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
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:99)
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:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

2 个答案:

答案 0 :(得分:1)

此代码修复了它:

    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContext context = getServletContext();
    InputSource xmlFile = new InputSource(context.getResourceAsStream("/WEB-INF/balle.xml"));
    try {
        xd = new XMLDoc(xmlFile);
    } catch (Exception e) {
        System.out.println("Something wrong happened");
    }
}

答案 1 :(得分:0)

这是由覆盖init(ServletContext上下文)而不是调用super.init(context)引起的。