为什么在编译 java servlet 时出现错误?
Why do I get error when compiling java servlet?
我创建了一个名为 TestingServlet 的 java servlet,但在尝试编译文件时出现错误。我已经为 servlet-api.jar 设置了类路径,并且还尝试在编译时包含类路径:
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
Tomcat 10 Servlet API 使用新的 jakarta
命名空间,因此包名称已更改,如 Tomcat 10 page:
中所述
Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.*
to jakarta.*
.
所以在你的情况下
import jakarta.servlet.*
import jakarta.servlet.http.*
我创建了一个名为 TestingServlet 的 java servlet,但在尝试编译文件时出现错误。我已经为 servlet-api.jar 设置了类路径,并且还尝试在编译时包含类路径:
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
Tomcat 10 Servlet API 使用新的 jakarta
命名空间,因此包名称已更改,如 Tomcat 10 page:
Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from
javax.*
tojakarta.*
.
所以在你的情况下
import jakarta.servlet.*
import jakarta.servlet.http.*