*** is not a jakarta.servlet.Servlet的解決方法


    前段時間因為工作原因需要在Jetty上部署app,直覺應該用最新的Jetty 11,但是部署之后Jetty啟動的過程中,會出現*** is not a jakarta.servlet.Servlet的錯誤。因為這個war包曾經在tomcat 8上成功部署,所以確定war沒問題,后來下載Jetty 10,成功把這個war包部署在Jetty 10上了。看了一下官方的有關聲明

 

 

 

 可能和這個jakarta有關。我想着可能和servlet-api包相關,需要導入的語法可能不一樣,但是因為這個war包是其他人給的,只能請求別人給修改代碼重新編譯,只好自己想辦法抄寫了一個簡單的servlet來驗證一下。順便說一下,需要的servlet-api.jar在tomcat里有,不用單獨下載。

下邊先說解決辦法

在tomcat 9和Jetty 10能部署成功war包部署到tomcat 10或Jetty 11上出現*** is not a jakarta.servlet.Servlet之類的錯誤。

1. 如果自己能得到源碼,把源文件中

import javax.servlet.*;
import javax.servlet.http.*; 

改成

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

重新編譯,替換.class文件,重啟tomcat 10或Jetty 11即可。

2. 如果改不了源碼,那就只能棄用tomcat 10和Jetty 11,改用tomcat 9和Jetty 10了。

 

現在來說驗證過程

1. 到官網https://tomcat.apache.org/下載tomcat 9和tomcat 10。
https://www.runoob.com/servlet/servlet-first-example.html
這里有tomcat的部署流程,一步一步跟着來,保證tomcat部署啟動正確。
2.如果Tomcat Windows上啟動有亂碼
修改conf\logging.properties
java.util.logging.ConsoleHandler.encoding = UTF-8
將 UTF-8 修改為 GBK,修改后的效果為:
java.util.logging.ConsoleHandler.encoding = GBK

3. 同樣可以從第一步的鏈接上找到HelloWorld這個簡單的servlet的例子,我直接抄下來了。

import java.io.*;
import java.servlet.*;
import java.servlet.http.*;

public class HelloWorld extends HttpServlet {
    private String message;
    public void init() throws ServletException {
        message = "Hello World";
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    public void destroy() {
    }
}

4. 用tomcat 9中的servlet-api.jar來編譯HelloWorld

javac -cp .;D:\software\apache-tomcat-9.0.41\lib\servlet-api.jar HelloWorld.java

5.部署hello
a. 在webapps下創建hello目錄
b. 在hello目錄下創建index.jsp,這個文件可以直接從webapps\ROOT里copy過來的
c. 在hello目錄下創建WEB-INF目錄,並創建web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>

</web-app>
View Code

d. 創建\classes目錄,並把編譯好的class文件放在這個目錄下,我這里全路徑為D:\software\apache-tomcat-9.0.41\webapps\hello\WEB-INF\classes
6. 重啟tomcat, console不出錯,並且可以訪問http://localhost:8080/hello/HelloWorld。
7. 將整個目錄copy下來放在Jetty 11的webapps目錄中
8. Jetty 11報錯
9. 將源文件中的import改成

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

10. 用tomcat 10中的servlet-api.jar來編譯HelloWorld

javac -cp .;D:\software\apache-tomcat-10.0.0\lib\servlet-api.jar HelloWorld.java

11. 將編譯好的class文件放到Jetty 11的webapps\hello\WEB-INF\classes目錄下

12. 重啟Jetty 11

13. console不報錯,並且能夠訪問http://localhost:8080/hello/HelloWorld

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM