【遇到的问题】Tomcat10中Servlet包名问题
我在创建Servlet程序的过程中,发现的问题:
自己写的Servlet类:
package com.wang.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("Hello,Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
在使用tomcat服务器,访问该Servlet类时,报了500的错误
错误描述:这个类不是Servlet
通过上网搜索,我得知是tomcat10服务器的原因,如下:
tomcat10将 javax.servlet-api 换为 jakarta.servlet-api
所以与我之前导入的依赖不匹配
解决方案:
-
将tomcat版本回退到9
-
导入新的依赖
-
在maven仓库Maven Repository: Search/Browse/Explore (mvnrepository.com)
搜索 jakarta.servlet-api
将该依赖导入到pom.xml中
-
导入后,在java类中,也需要将包名改为jakarta.servlet
import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
-
然后解决了