为什么在编译Java Servlet时会出现错误?

发布于 2025-01-20 14:32:48 字数 1820 浏览 0 评论 0 原文

我创建了一个名为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

I have created a java servlet named TestingServlet and get error when I try to compile file. I have set the classpath for servlet-api.jar and also tried including classpath when compiling:

javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar" TestingServlet.java

Code is :

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>");
 }
}

Error is:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2025-01-27 14:32:48

tomcat 10 Servlet API使用新的 jakarta 命名空间,因此包装名称更改,如 Tomcat 10 Page

tomcat 10的用户应注意,由于将Java EE转移到Eclipse Foundation的一部分,由于从Java EE到Jakarta EE转移,所有实施API的主要软件包已从 javax。* to jakarta。*

所以在你的情况下

import jakarta.servlet.*
import jakarta.servlet.http.*

Tomcat 10 Servlet API uses the new jakarta namespace, so the package names changed, as explained in the 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.*.

So in your case

import jakarta.servlet.*
import jakarta.servlet.http.*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文