【发布时间】:2019-03-23 09:01:26
【问题描述】:
我是 Java Web 开发的新手。我正在尝试在 Apache Tomcat (9.0.17) 中部署 Web 应用程序。我已经安装了预装了 Java OpenJDK 的 Linux 操作系统。我无法编译 JSP。
终端中“java -version”的输出:
openjdk version "1.8.0_202"
OpenJDK Runtime Environment (build 1.8.0_202-b26)
OpenJDK 64-Bit Server VM (build 25.202-b26, mixed mode)
我已将我的 java 包放在 TOMCAT_INSTALLATION_DIR/webapps/app/WEB_INF/classes/ 中。这里 TOMCAT_INSTALLATION_DIR 是我提取 apache-tomcat-9.0.17.tar.gz 文件的目录。 在类目录中,我有一个名为 abc 的 java 包。在里面,我放了 Test.java
这是Test.java中的代码
package abc;
public class Test{
public String f(){
return ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}
public Test(){
System.out.println("I am created");
}
}
我成功编译它并放入我的 Test.java 文件所在的同一目录(在 abc 目录内)。
我在 WEB-INF 文件夹中创建了 web.xml。 web.xml 中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>app</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我在 app/ 目录中有 index.jsp 文件。 JSP 内容:
<%@ page import="abc.*"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Test cc=new Test();
cc.f();
%>
</body>
</html>
我在安装目录内的 bin 目录中通过 ./cataline.sh run 命令运行 apache。然后我打开 localhost:8080/app URI。我遇到了错误
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9: <%
10: Test cc=new Test();
11: cc.f();
12: %>
13: </body>
An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9: <%
10: Test cc=new Test();
11: cc.f();
12: %>
13: </body>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
【问题讨论】: