JavaWeb-servlet-mapping

Mapping问题

一个servlet可以指定一个映射路径

<!-- 注册Servlet-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.qk.servlet.HelloServlet</servlet-class>
  </servlet>
  <!-- Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s2</url-pattern>
  </servlet-mapping>
  • 访问路径为: http://localhost:8080/s/

一个servlet可以指定多个映射路径

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.qk.servlet.HelloServlet</servlet-class>
  </servlet>
  <!-- Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s3</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s4</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s5</url-pattern>
  </servlet-mapping>
  • 访问路径为:http://localhost:8080/s/s5
  • 访问路径为:http://localhost:8080/s/s4
  • 访问路径为:http://localhost:8080/s/s3
  • 访问路径为:http://localhost:8080/s/s2
  • 都可以访问到同一个servlet

一个servlet可以指定通用映射路径

<!-- 注册Servlet-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.qk.servlet.HelloServlet</servlet-class>
  </servlet>
  <!-- Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s2/*</url-pattern>
  </servlet-mapping>
  • 访问路径为:http://localhost:8080/s/s2/sgesdg
  • s2/ 下的任意字符都可以通配到servlet

指定一些后缀或者前缀等等…

<!-- 注册Servlet-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.qk.servlet.HelloServlet</servlet-class>
  </servlet>
  <!-- Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>*.qk</url-pattern>
  </servlet-mapping>
  • 访问路径为:http://localhost:8080/s/s2/sgesdg.qk
  • 访问路径为:http://localhost:8080/s/dfbzsf.qk
  • 只要后缀是 .qk 的网址,无论嵌套多少级,都会访问到指定servlet

精准大于默认(通配)

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入doGet方法");
        PrintWriter writer = resp.getWriter();
        writer.print("hello,servlet-02");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
<!-- 注册Servlet-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.qk.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>erro</servlet-name>
    <servlet-class>com.qk.servlet.HelloErro</servlet-class>
  </servlet>
  <!-- Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/s3</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>erro</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  • 精准的url可以忽略掉通配的影响
  • 当访问 http://localhost:8080/s/s3时访问的是Helloservlet,但只要不是s3的任何网址,都会被通配符影响跳转到HelloErro(404)
上一篇:一文搞懂 ElasticSearch 之 Mapping


下一篇:Gateway工作机制