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

采用注解方式->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

作者:compass

出处:https://www.cnblogs.com/forwardCompass/p/17480675.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   amazingCompass_s  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示