码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet相关配置

时间:2020-02-19 01:17:04      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:web   多个   throw   pre   dog   public   相关   extends   span   

配置方式

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相关配置

标签:web   多个   throw   pre   dog   public   相关   extends   span   

原文地址:https://www.cnblogs.com/rijiyuelei/p/12329485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!