需要帮助理解Tomcat servlet中的异常

时间:2013-05-25 09:19:50

标签: java tomcat servlets servlet-filters

我有以下例外:

Servlet.service() for servlet indexpage threw exception java.lang.NullPointerException
at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:675)
at org.apache.catalina.servlets.DefaultServlet.doHead(DefaultServlet.java:351)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
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 planplatform.cors.CorsFilter.doFilter(CorsFilter.java:61)
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.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Unknown Source)

此异常中唯一的代码是CorsFilter:

HttpServletRequest req = (HttpServletRequest) servletRequest;
    HttpServletResponse resp = (HttpServletResponse) servletResponse;

    String origin = req.getHeader("Origin");

    boolean shouldAllowCorsOnOrigin = allowCorsOnOrigin(origin);

    if (shouldAllowCorsOnOrigin) 
    {
        resp.addHeader("Access-Control-Allow-Origin", origin);
        resp.addHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");

        String headers = req.getHeader("Access-Control-Request-Headers");
        String method = req.getHeader("Access-Control-Request-Method");
        resp.addHeader("Access-Control-Allow-Methods", method);
        resp.addHeader("Access-Control-Allow-Headers", headers);

        if ("options".equalsIgnoreCase(req.getMethod())) 
        {
            resp.setContentType("text/plain");

            resp.getWriter().flush();
            return;
        }
    }

    // Fix ios6 caching post requests
    if ("post".equalsIgnoreCase(req.getMethod())) {
        resp.addHeader("Cache-Control", "no-cache");
    }

    if (filterChain != null) {
        filterChain.doFilter(req, resp);
    }

shouldAllowCorsOnOrigin为false,因此代码不会输入大的if。

有关异常发生的任何想法吗?

2 个答案:

答案 0 :(得分:1)

我将获得Tomcat的版本号,并查找该版本的DefaultServlet的源代码。我查了6.0.26,但堆栈跟踪中的行号与可能引发此错误的行不一致。

DefaultServlet中抛出错误的方法取决于从其自己的上下文或JNDI查找获得的资源。进一步查看日志中的异常,并以“No resources”作为消息。 DefaultServlet.init()方法使用两个常量来查找资源 - 服务器环境是否有其中一个资源?

答案 1 :(得分:0)

您应该查看indexpage servlet,可能存在问题,因为在您的堆栈跟踪中,您在Servlet.service()期间有空指针,并且servlet容器调用此方法以允许servlet响应请求。在执行init方法之前(在indexpage处)。你能告诉我们indexpage servlet吗?