【问题标题】:Null pointer exception: HTTP response code 500空指针异常:HTTP 响应代码 500
【发布时间】:2015-10-30 12:55:06
【问题描述】:

尝试以编程方式运行 Web 应用程序。编写了将自动部署 Web 应用程序的代码。部署 Web 应用程序后,我使用 URL 类调用带有一些输入的 servlet 并打印响应,如下所示。

    //code to start Tomcat server and deploy the web app
    String myUrl = "http://localhost:8080/CrossSiteScripting?user=shri&passwd=firstPassword";
    URL url = new URL(myUrl); 
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    String httpMethod = con.getRequestMethod();
    while((inputLine=in.readLine()) != null){
        response.append(inputLine);
    }
    in.close();
    System.out.println("\n----------------------\nHTTP Method : "+ httpMethod +"\nOutput: "+response.toString());

我正在调用 servlet 的 doGet() 方法。 doGet 方法内的代码如下所示。

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String user = request.getParameter("user");
      String pwd = request.getParameter("passwd");
      request.setAttribute("user", user);
      request.setAttribute("passwd", pwd);
      PrintWriter pw = response.getWriter();
      pw.write("Inside doGet method of Servlet\nuser name: "+user+"password= "+pwd);
      RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
      view.forward(request, response);
}

如果我注释掉 doget 方法中的最后一行 view.forward(request, response);,那么我就能得到预期的响应。但如果它没有被评论,那么我得到NullPointerException 并且服务器返回HTTP 响应代码500。
这是我在使用 URL 类运行应用程序时遇到的错误。

    asAug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardContext setPath
    WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
    C:\Users\IBM_ADMIN\workspace\Initiate Tomcat\.
    Host: StandardEngine[Tomcat].StandardHost[localhost]

    starting server...
    Aug 07, 2015 11:53:47 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-nio-8080"]

    Aug 07, 2015 11:53:47 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector

    INFO: Using a shared selector for servlet write/read

    Aug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardService startInternal

    INFO: Starting service Tomcat

    Aug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardEngine startInternal

    INFO: Starting Servlet Engine: Apache Tomcat/8.0.23

    Aug 07, 2015 11:53:48 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [173] milliseconds.
    Aug 07, 2015 11:53:48 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-nio-8080"]

    started.
    Initiating URL: http://localhost:8080/CrossSiteScripting?user=shri&user=manoj&user=shrikant&passwd=firstPassword&passwd=admino
    Aug 07, 2015 11:53:48 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [CrossSiteScripting] in context with path [/] threw exception
    java.lang.NullPointerException
        at penetration.testing.CrossSiteScripting.doGet(CrossSiteScripting.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1177)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:857)

    Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/CrossSiteScripting?user=shri&user=manoj&user=shrikant&passwd=firstPassword&passwd=admino
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1638)
at main.java.launch.Main.main(Main.java:92)

由于view.forward() 方法,最后一行可能会抛出异常。哪个将控件转发给JSP. 有没有其他方法可以做到这一点?我希望该 JSP 页面能够像在浏览器中打开时那样执行。

这是目录结构:

    +InitiateTomcat
    |-- +src
    |---|- +main.java.launch
    |---|--- main.java //the main class from where I start Tomcat server and deploy a web application
    |---|- +penetration.testing
    |---|--- CrossSitrScripting.java // the servlet which am calling
    |-- +WebContent
          +WEB-INF
             homePage.jsp  // the homepage

【问题讨论】:

  • @Javy @Garry :我尝试将其更改为/homepage.jsp,但它不起作用。得到同样的错误。
  • @Javy 错误是因为最后一行 view.forward. 因为我尝试在没有该行的情况下运行它并且它成功执行。

标签: java tomcat servlets web-applications


【解决方案1】:

500 http状态码表示服务器无法满足请求,发生了“意外”错误,我猜是无法通过该URL访问页面“homePage.jsp”,尝试直接通过浏览器访问验证。

【讨论】:

  • 我可以使用浏览器访问它
【解决方案2】:

我猜错误或异常在这里

 RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");

像homePage.jsp这样的访问页面应该是一个空字符串或者以'/'开头,如果你直接写homePage.jsp,路径是:

C:\Users\IBM_ADMIN\workspace\Initiate Tomcat\homePage.jsp

所以你应该这样做

RequestDispatcher view = request.getRequestDispatcher("/homePage.jsp");

【讨论】:

    【解决方案3】:

    你在CrossSiteScripting.java:41得到空指针

    你说的是:

    view.forward(request, response);
    

    所以这个生成的视图是空的,因为找不到homePage.jsp

    RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
    

    如果您的homePage.jsp 与当前页面的相对位置相同,则需要使用“homePage.jsp”,如果homePage.jsp 在根目录中,则应使用正斜杠“/”指向根因此“/homePage.jsp”

    我假设您应该在转发时在 jsp 调用前面使用正斜杠,如下所示:

    RequestDispatcher view = request.getRequestDispatcher("/homePage.jsp");
    

    【讨论】:

    • 我也认为它无法找到homePage.jsp. 我尝试将其更改为/homePage.jsp,但仍然无法正常工作。我还尝试将其更改为/InitiateTomcat/homePage.jsp,其中InitiateTomcat 是我的项目的名称(根)。我将添加项目的目录结构。这可能有助于您更好地理解。
    • @Manoj.. 是的,这会有所帮助
    • 我已经添加了目录结构
    • @Manoj .. 你能试试request.getRequestDispatcher( "/WEB-INF/homePage.jsp"
    • @Manoj ...根据对另一个答案的评论,您说您可以使用浏览器访问 homePage.jsp。你能添加那个网址吗?
    【解决方案4】:

    WEB-INF 目录中的任何内容都不能通过 URL 直接访问。 请将 homePage.jsp 文件移到 WEB-INF 目录之外,然后尝试访问它。我希望它会起作用。

    问候 桑迪普

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2016-01-14
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多