Servlet相关配置

Posted rijiyuelei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet相关配置相关的知识,希望对你有一定的参考价值。

配置方式

webXML

定义标签<urlpartten>Servlet访问路径

 

注解

定义的<urlpartten>数组:可以为一个servlet定义多个访问路径。

 1 package cn.itcast.web.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 /**
11  * @author 旗木五五开
12  * @create 2020-02-19 0:08
13  */
14 @WebServlet({"/d4","/dd4","/ddd4"})
15 //{}多个,数组用大括号
16 public class ServletDemo4 extends HttpServlet {
17     @Override
18     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19         System.out.println("doGet");
20     }
21 }

 

技术图片

 

 技术图片

技术图片

技术图片

 

 路径定义规则

  1. /xxx
  2. /xxx/xxx:多层路径。目录结构
  3. /*.do

多层路径。目录结构

 1 package cn.itcast.web.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 /**
11  * @author 旗木五五开
12  * @create 2020-02-19 0:08
13  */
14 @WebServlet("/user/demo4")
15 public class ServletDemo4 extends HttpServlet {
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18         System.out.println("doGet");
19     }
20 }

技术图片

技术图片

 

 *代表任意,想写啥就写啥,写啥都能访问到。直接写/*。后面写什么都能访问到

优先级最低,别的都访问不到,才访问

 1 package cn.itcast.web.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 /**
11  * @author 旗木五五开
12  * @create 2020-02-19 0:08
13  */
14 @WebServlet("/user/*")
15 public class ServletDemo4 extends HttpServlet {
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18         System.out.println("doGet");
19     }
20 }

技术图片

 技术图片

 

*.do:*所有,do后缀名。注意不要写错,*.啥都行

注意*前不要加/

加了就——报错

 1 package cn.itcast.web.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 /**
11  * @author 旗木五五开
12  * @create 2020-02-19 0:08
13  */
14 @WebServlet("*.do")
15 public class ServletDemo4 extends HttpServlet {
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18         System.out.println("doGet");
19     }
20 }

 技术图片

 

 技术图片

 

以上是关于Servlet相关配置的主要内容,如果未能解决你的问题,请参考以下文章

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

Servlet相关配置

[JavaWeb-Servlet]Servlet相关配置

servlet,filter,listener,intercepter区别

Servlet相关学习