为什么在编译Java Servlet时会出现错误?
我创建了一个名为TestingServlet的Java Servlet,并在尝试编译文件时会出现错误。我已经为Servlet-api.jar设置了classPath,并在编译时也尝试了包括class路径:
javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar" TestingServlet.java
代码为:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestingServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}
错误是:
TestingServlet.java:6: error: cannot find symbol
public class TestingServlet extends HttpServlet {
^
symbol: class HttpServlet
TestingServlet.java:8: error: cannot find symbol
public void doGet(HttpServletRequest request,
^
symbol: class HttpServletRequest
location: class TestingServlet
TestingServlet.java:9: error: cannot find symbol
HttpServletResponse response)
^
symbol: class HttpServletResponse
location: class TestingServlet
TestingServlet.java:10: error: cannot find symbol
throws ServletException, IOException {
^
symbol: class ServletException
location: class TestingServlet
TestingServlet.java:1: error: package javax.servlet does not exist
import javax.servlet.*;
^
TestingServlet.java:2: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
6 errors
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tomcat 10 Servlet API使用新的
jakarta
命名空间,因此包装名称更改,如 Tomcat 10 Page :所以在你的情况下
Tomcat 10 Servlet API uses the new
jakarta
namespace, so the package names changed, as explained in the Tomcat 10 page:So in your case