简体   繁体   English

Tomcat 10 在 Mac 上编译 servlet 时无法识别 servlet-api.jar

[英]Tomcat 10 on Mac not Recognizing servlet-api.jar when compiling servlet

I'm working on creating a Servlet for a tomcat webpage and the Servlet will not compile.我正在为 tomcat 网页创建一个 Servlet,但该 Servlet 无法编译。

I'm using the servlet-api.jar & mysql-connector.8.0.28.jar for the servlet, and there are also several JavaBeans I have created to store information being passed between the database and the page.我正在为 servlet 使用 servlet-api.jar & mysql-connector.8.0.28.jar,我还创建了几个 JavaBeans 来存储在数据库和页面之间传递的信息。

The directory structure for tomcat is as follows: tomcat的目录结构如下:

  • all.jar files are in./Tomcat/tomcat-10/lib/ all.jar个文件在./Tomcat/tomcat-10/lib/
  • the servlet to be compiled is stored at./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/要编译的 servlet 存储在./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/
  • the beans are stored at./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/centerStage/ bean 存储在./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/centerStage/

This is the command I use to compile everything:这是我用来编译所有内容的命令:

javac -cp .:./Tomcat/tomcat-10/lib/servlet-api.jar:./Tomcat/tomcat-10/lib/mysql-connector-java-8.0.28.jar -classpath ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/:./Tomcat/tomcat-10/lib/ ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java

When I try to compile the servlet I get 9 Errors pointing to the HttpServlet class:当我尝试编译 servlet 时,出现 9 个指向 HttpServlet class 的错误:

./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:32: error: cannot find symbol
public class CenterStageServlet extends HttpServlet {
                                        ^
  symbol: class HttpServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:25: error: cannot find symbol
@WebServlet(name = "CenterStageServlet", 
 ^
  symbol: class WebServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:41: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                     ^
  symbol:   class HttpServletRequest
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:41: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                 ^
  symbol:   class HttpServletResponse
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:42: error: cannot find symbol
         throws IOException, ServletException {
                             ^
  symbol:   class ServletException
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.*;             // Tomcat 10
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:7: error: package jakarta.servlet.http does not exist
import jakarta.servlet.http.*;
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:8: error: package jakarta.servlet.annotation does not exist
import jakarta.servlet.annotation.*;  // Tomcat 10
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:40: error: method does not override or implement a method from a supertype
   @Override
   ^
9 errors

Servlet Code:服务程序代码:

package centerStage;

// To save as "<TOMCAT_HOME>\webapps\CenterStage\WEB-INF\classes\CenterStageServlet.java"
import java.io.*;
import java.sql.*;
import jakarta.servlet.*;             // Tomcat 10
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;  // Tomcat 10
//import javax.servlet.*;             // Tomcat 9
//import javax.servlet.http.*;
//import javax.servlet.annotation.*;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.time.LocalDate;

import centerStage.*;
 
@WebServlet(name = "CenterStageServlet", 
            urlPatterns = {"/appointment", "/availibility", "/barber", "/client", "/service"})    
            // Configure the request URL for this servlet (Tomcat 7/Servlet 3.0 upwards)


public class CenterStageServlet extends HttpServlet {

    // Variables
    
   // The doGet() runs once per HTTP GET request to this servlet.
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
 
       // Do some processing
   }

   // Other Methods
}

I've verified that the.jar's that I'm looking for are in that /lib/ directory.我已经确认我要查找的 .jar 文件位于该 /lib/ 目录中。

Compiling other Webapps in this Tomcat directory don't give me any issues, so I'm not sure what the problem is.在这个 Tomcat 目录中编译其他 Webapps 没有给我任何问题,所以我不确定问题是什么。

Any help/suggestions would be great!任何帮助/建议都会很棒!

Okay, turns out it was something really simple.好吧,事实证明这真的很简单。

When I moved the servlet from ~Tomcat/webapps/CenterStage/WEB-INF/classes/ to ~Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/ and then tried to compile with javac -cp.:~/Tomcat/lib/servlet-api.jar:~/Tomcat/lib/mysql-connector-java-8.0.28.jar./Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/*.java everything worked fine.当我将 servlet 从 ~Tomcat/webapps/CenterStage/WEB-INF/classes/ 移动到 ~Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/ 然后尝试用 javac -cp.:~/Tomcat/ 编译lib/servlet-api.jar:~/Tomcat/lib/mysql-connector-java-8.0.28.jar./Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/*.java 一切正常。

So basically, I should have had the Servlet.java in the same directory as the JavaBeans and everything works fine.所以基本上,我应该将 Servlet.java 放在与 JavaBeans 相同的目录中,并且一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM