码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-01-29 12:02:21      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:open   apach   message   use   width   jar   -name   div   vax   

    前段时间因为工作原因需要在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

 

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

标签:open   apach   message   use   width   jar   -name   div   vax   

原文地址:https://www.cnblogs.com/panda4671/p/14341576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!