采用注解方式-HTTP Status 404 – Not Found 或者 采用web

发布时间 2023-06-15 15:32:12作者: amazingCompass_s

采用注解方式->HTTP Status 404 – Not Found 或者 采用web.xml方式->cannot resolve the class in the tag

运行一个简单的servlet程序,分别采用了两种方式对servlet进行映射,均报错。

1⃣️采用Annotation方式

@WebServlet(name = "DownloadServlet",urlPatterns = "/download")
public class DownloadServlet extends HttpServlet {
  //todo
}

代码中不报错,但是访问 http://localhost:8080/Servlet/download?filename=abc.txt 会出现404

2⃣️采用web.xml方式进行servlet映射,servlet-class标签会直接报错

Cannot resolve class 'DownloadServlet'

原因竟然是存放DownloadServlet的src目录为Test Resource Root(绿色)

将其修改为Resource Root(蓝色)即可正常运行。

真是一个奇怪的错误,找了我小半天。

参考 https://stackoverflow.com/questions/70912031/java-servlet-web-xml-file-cannot-resolve-the-class-in-the-servlet-class-tag-in