【问题标题】:Request dispatcher is throwing nullPointerException?请求调度程序正在抛出 nullPointerException?
【发布时间】:2014-07-25 05:07:43
【问题描述】:

我正在使用请求调度程序,它正在抛出空指针异常。我不知道为什么会这样,因为我在根目录中有 Map.jsp。这是代码

LocationTrackerServlet.class

RequestDispatcher rd= getServletContext (). getRequestDispatcher ("/Map.jsp");// this is line 112
        rd.forward(request, response);

Tomcat 错误

【问题讨论】:

    标签: java jakarta-ee servlets tomcat7


    【解决方案1】:

    由于 GenericServlet 中抛出异常,似乎 servlet 配置未存储在 GenericServlet。看起来您正在覆盖 servlet init 方法并且错过了对 super.init 的调用以将配置存储在 GenericServlet 中。

    GenericServlet 在扩展 HttpServlet 时由您的 servlet 扩展。

    public void init(ServletConfig config) throws ServletException {  
        super.init(config);
        //Your code  
    }
    

    【讨论】:

    • 是的,这就是原因。
    【解决方案2】:

    使用这个:

    RequestDispatcher rd= request.getRequestDispatcher ("/Map.jsp");
    

    而不是:

    RequestDispatcher rd= getServletContext (). getRequestDispatcher ("/Map.jsp");
    

    【讨论】:

      【解决方案3】:

      使用 request.getRequestDipatcher()

      【讨论】:

      • 正确定义的 cmets 不会影响正常运行的代码,这就是它们的重点。
      • 这些cmets我没有实物,这里只供大家理解。
      • @user3621470 不,他们绝对不能,发布一个示例/询问您认为发生这种情况的位置。因为他们绝对不能。
      【解决方案4】:

      为什么要使用getServletContext().getRequestDipatcher,

      改为使用 request.getRequestDipatcher()

      【讨论】:

      • 这行得通,但为什么 getServletContext ().getRequestDipatcher 不起作用。
      • getServletContext ().getRequestDispatcher() 用于将请求转发到同一容器中的其他应用程序(上下文)...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      相关资源
      最近更新 更多