Servlet的urlPattern配置及XML配置Servlet
原创
©著作权归作者所有:来自51CTO博客作者夏志121的原创作品,请联系作者获取转载授权,否则将追究法律责任
目录
一、urlPattern配置
1、一个Servlet,可以配置多个urlPattern
2、urlPattern配置规则
① 精确匹配:
② 目录匹配:
③ 扩展名匹配:
④ 任意路径:
/ 和 /* 区别:
优先级:
二、XML配置方法编写Servlet
1、编写Servlet类
2、在web.xml中配置该Servlet
一、urlPattern配置
Servlet要想被访问,必须配置其访问路径(urlPattern)
1、一个Servlet,可以配置多个urlPattern
@WebServlet(urlPatterns = {"/demo1","demo2"})
2、urlPattern配置规则
① 精确匹配:
配置路径:@WebServlet ("/user/select")
访问路径:localhost:8080/web-demo/user/select
代码示例:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* uslPattern
* 精确匹配
*/
@WebServlet(urlPatterns = "/user/select")
public class ServletDemo8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo8 get...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
运行结果:URL:
控制台:
下面的代码示例运行结果,与以上结果,大同小异,只是配置路径不同。
② 目录匹配:
配置路径:@WebServlet ("/user/*")
访问路径:localhost:8080/web-demo/user/aaa
user/bbb
代码示例:
/**
* uslPattern
* 目录匹配:/user/*
*/
@WebServlet(urlPatterns = "/user/*")
public class ServletDemo9 extends HttpServlet {
③ 扩展名匹配:
配置路径:@WebServlet ("*.do")
访问路径:localhost:8080/web-demo/aaa.do
bbb.do
代码示例:
/**
* uslPattern
* 扩展名匹配:*.do
*/
@WebServlet(urlPatterns = "*.do")
public class ServletDemo10 extends HttpServlet {
④ 任意路径:
配置路径:@WebServlet ("/")
@WebServlet ("/*")
访问路径:localhost:8080/web-demo/hehe
haha
代码示例:
/**
* uslPattern
* 任意匹配:/ 及 /*
*/
@WebServlet(urlPatterns = "/")
public class ServletDemo11 extends HttpServlet {
/ 和 /* 区别:
当我们的项目中的Servlet配置了"/",会覆盖调tomcat中的DefaultServlet,当其他的url-pattern都匹配不上时,都会走这个Servlet
当我们的项目配置了"/*",意味着匹配任意访问路径
优先级:
精确路径 > 目录路径 > 扩展名路径 > /* > /
二、XML配置方法编写Servlet
Servlet从3.0版本后开始支持使用注解配置,3.0版本前只支持XML配置文件的配置方法
步骤:
1、编写Servlet类
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo12 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo12...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
2、在web.xml中配置该Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--
Servlet 全类名
-->
<servlet>
<servlet-name>demo12</servlet-name>
<servlet-class>com.itheima.web.request.ServletDemo12</servlet-class>
</servlet>
<!--
Servlet 访问路径
-->
<servlet-mapping>
<servlet-name>demo12</servlet-name>
<url-pattern>/demo12</url-pattern>
</servlet-mapping>
</web-app>
执行结果:URL:
控制台: