Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

tomcat - Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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:

java.lang.ClassCastException: class com.example.controllers.BeerSelect
                       cannot be cast to class jakarta.servlet.Servlet

The jakarta.servlet.Servlet is part of Servlet API version 5.0 which in turn is part of Jakarta EE version 9. Your servlet is actually extending from javax.servlet.Servlet which in turn is part of an older JEE version which is actually not supported by your target runtime (Tomcat 10.x).

You have 2 options:

  1. Replace the javax.servlet.* imports in your code by jakarta.servlet.* ones.

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

    Then you can just compile against the libraries from a Servlet 5.0 based target runtime.

  2. Or, downgrade the servlet container from Servlet API version 5.0 to a previous version, at least the one still having the javax.servlet.* package name. Tomcat 9.x is the latest one still having the old package.

The technical reason is that during the step from Java/Jakarta EE 8 to Jakarta EE 9 all javax.* packages have been renamed to jakarta.* packages. So there is no backwards compatibility anymore since Jakarta EE 9.

So, when following any servlet tutorials which still use the old javax.servlet.* package while you're using Tomcat 10 or newer, then you need to manually swap out the package used in code examples for the jakarta.servlet.* one. Generally it'll work just fine.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...