HTTP Status 500 - Unable to compile class for JSP
好的,有很多关于这个错误的文章,我还是个新手。我认为这个错误可能是由某种不同的原因引起的,这是我的(可能是愚蠢的或已经存在的)原因:
它没有找到显然是从 NetBeans 导入的类。
<%@ page import="mypack.Display" %>
事实上,如果我查看缓存文件“index_jsp.java”,问题就在“ import mypack.Display; ”所在的行上。我的问题与此如此相似是什么原因导致此 jsp 错误 500?不同的是我没有将mypack.Display放入默认包中,那么我不知道为什么会出现这个错误。
这是我的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><% out.print("jsp class test");%></h1>
<%@ page import="mypack.Display" %>
<%
Display t = new Display();
t.test("Roby");
%>
</body>
</html>
这是简单的 Display.java
package mypack;
public class Display{
public void test(String msg){
System.out.println("My name is "+msg);
}
}
我的一个朋友说我应该将servlet配置到web.xml文件到 WEB-INF 文件夹中,但是查看示例我不明白我该怎么做。我必须将包映射到该文件吗?
我将Display.java放在 Netbeans 正确采用的包“mypack”下,事实上,如果我尝试更改 index.jsp 和 Display.java 中的类或包名称,编译器会显示错误。
有人可以帮助我更好地理解吗?
在完整的错误下方:
type Exception report
message Unable to compile class for JSP:
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:
An error occurred at line: 14 in the generated java file
Only a type can be imported. mypack.Display resolves to a package
An error occurred at line: 19 in the jsp file: /index.jsp
Display cannot be resolved to a type
16: <%--<jsp:useBean id="link" scope="application" class = "mypack.Display" />--%>
17: <%@ page import="mypack.Display" %>
18: <%
19: Display t = new Display();
20: t.test("Roby");
21: // out.println(outpt);
22: %>
An error occurred at line: 19 in the jsp file: /index.jsp
Display cannot be resolved to a type
16: <%--<jsp:useBean id="link" scope="application" class = "mypack.Display" />--%>
17: <%@ page import="mypack.Display" %>
18: <%
19: Display t = new Display();
20: t.test("Roby");
21: // out.println(outpt);
22: %>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs.