【servlet】Servlet urlPattern配置

Servlet要想被访问,必须配置其访问路径(urlPattern)

  1. 一个Servlet,可以配置多个urlPattern
@WebServlet(urlPatterns={"/demo1","/demo2"})
  1. urlPattern配置规则:
  • 精确匹配

    就是指配置的路径和访问的路径完全一致

    • 配置路径:@WebServlet("/user/select")
    • 访问路径:localhost:8080/web-demo/user/select
  • 目录匹配

    • 配置路径:@WebServlet("/user/*")
    • 访问路径:localhost:8080/web-demo/user/aaa`localhost:8080/web-demo/user/bbb`
  • 扩展名匹配

    • 配置路径:@WebServlet("*.do")
    • 访问路径:localhost:8080/web-demo/aa.do`localhost:8080/web-demo/user/bb.do`
  • 任意匹配

    • 配置路径:@WebServlet("/")`@WebServlet("/*")`
    • 访问路径:localhost:8080/web-demo/hehe`localhost:8080/web-demo/user/haha`

    //*的区别:

    当项目中的Servlet配置了”/”,会覆盖掉tomcat中的DefaultServlet,当其他的url-pattern都匹配不上时都会走这个Servlet

    当项目中配置了”/”,意味着匹配任意访问路径,也就是说”/”的优先级更高

  1. 优先级

    精确路径>目录路径>扩展名路径>/*>/

posted @   ShaunY  阅读(114)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示