在WebServlet的配置里面urlPattern的类型是一个String数组 ,因此注解里面的参数设置可以如下
@WebServlet(urlPatterns = {"/demo7","/demo8"})
public class ServletDemo7 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo7 get...");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
精确路径>目录路径>扩展名路径> /* > /
这种方式必须必须全部都一样
/*** urlPattern:* * 精确匹配*/@WebServlet(urlPatterns = "/user/select")
public class ServletDemo8 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo8 get...");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
这个匹配不管user后面写什么都会匹配到这个页面,这个匹配不会和精确上匹配相冲突,
/*** urlPattern:* * 目录匹配:/user/**/@WebServlet(urlPatterns = "/user/*")
public class ServletDemo9 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo9 get...");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
这个如果写成@WebServlet(urlPatterns = "*.do")项目启动就会报错
/*** urlPattern:* * 扩展名匹配: *.do*/@WebServlet(urlPatterns = "*.do")
public class ServletDemo10 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo10 get...");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
/*优先级高于/
使用/会导致静态资源无法访问也就是会覆盖默认servlet
/*** urlPattern:* * 任意匹配:/或者/**///@WebServlet(urlPatterns = "/")
//@WebServlet(urlPatterns = "/*")
public class ServletDemo11 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo11 get...");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
这里需要在web.xml配置servlet的全类名和servlet的映射