I am trying to compile and deploy a simple web app from command line.
servlet-api.jar from Apache Tomcat does not compile my java file, but javax.servlet-api-4.0.1 from the maven central repository compiles it successfully. Even so, I get an error when I deploy the app and try to use it in the browser.
I am using:
- javac 11.0.8
- Apache Tomcat 10.0 (servlet-api.jar 5.0)
Java file:
package com.example.controllers;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}
When I try to compile it with the servlet-api.jar I get:
public class BeerSelect extends HttpServlet {
^
symbol: class HttpServlet
srccomexamplecontrollersBeerSelect.java:9: error: cannot find symbol
public void doPost(HttpServletRequest request,
^
symbol: class HttpServletRequest
location: class BeerSelect
srccomexamplecontrollersBeerSelect.java:10: error: cannot find symbol
HttpServletResponse response)
^
symbol: class HttpServletResponse
location: class BeerSelect
srccomexamplecontrollersBeerSelect.java:11: error: cannot find symbol
throws IOException, ServletException {
^
symbol: class ServletException
location: class BeerSelect
srccomexamplecontrollersBeerSelect.java:3: error: package javax.servlet does not exist
import javax.servlet.*;
^
srccomexamplecontrollersBeerSelect.java:4: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
6 errors
However, javax.servlet-api-4.0.1 compiles the file successfully. Note: I've already tested and ruled out command-line command as a possible cause of the problem.
When I place the .class file in the corresponding Tomcat directory, start the server and try to interact with the app, I get the following exception:
Exception
jakarta.servlet.ServletException: Error instantiating servlet class [com.example.controllers.BeerSelect]
Root Cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
Root Cause
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
I tried placing the javax.servlet-api-4.0.1 in the Tomcat/lib directory, but then I get:
Exception
jakarta.servlet.ServletException: Class [com.example.controllers.BeerSelect] is not a Servlet
Root Cause
java.lang.ClassCastException: class com.example.controllers.BeerSelect cannot be cast to class jakarta.servlet.Servlet (com.example.controllers.BeerSelect is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7862f56; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @4b4523f8)
Not sure the last makes any sense, but I ran out of ideas.
Any help is more than welcome!
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…