博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析...
阅读量:5236 次
发布时间:2019-06-14

本文共 6303 字,大约阅读时间需要 21 分钟。

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

多个请求对应一个Servlet解析

如果我们每一个请求对应一个Servelt,这样的话,代码就显得比较臃肿,最主要的是,也不方便系代码的管理和优化

 

方式一:

  

 

  1. 思路:对于每一个页面请求我们设置成一个对应的方法,并且为请求的url设置相应的method参数,servlet-mapping 为  @WebServlet("/customerServlet")  ,而在Servlet 中通过获取 method 参数不同的取值,通过一个switch 语句选择不同的方法,同时调用不同的方法。

  

  2. 代码:test1.jsp ,  CustomerServlet1.java

 

  test.jsp

 

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6 
7 测试 8 9 10 11 Add112
13 14 Query115
16 17 Delete118
19
20
21 22 23 24 25

 

 

  CustomerServlet1.java

 

1 package com.jason.mvcapp.servlet; 2  3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /**11  * Servlet implementation class CustomerServlet12  */13 @WebServlet("/customerServlet")14 public class CustomerServlet1 extends HttpServlet {15     private static final long serialVersionUID = 1L;16 17     /**18      * 19      */20     protected void doGet(HttpServletRequest request,21             HttpServletResponse response) throws ServletException, IOException {22         doPost(request, response);23     }24 25     protected void doPost(HttpServletRequest request,26             HttpServletResponse response) throws ServletException, IOException {27 28         String method = request.getParameter("method");29 30         switch (method) {31         case "add":32             add(request, response);33             break;34         case "query":35             query(request, response);36             break;37         case "delete":38             delete(request, response);39             break;40         }41 42     }43 44     private void delete(HttpServletRequest request, HttpServletResponse response)45             throws ServletException, IOException {46         System.out.println("delete");47 48     }49 50     private void query(HttpServletRequest request, HttpServletResponse response)51             throws ServletException, IOException {52         System.out.println("query");53 54     }55 56     private void add(HttpServletRequest request, HttpServletResponse response)57             throws ServletException, IOException {58         System.out.println("add");59     }60 61 }

 

 

 

 

方法二:

 

  1. 思路:将所有的请求都设置成 方法名.do, 而将 servlet-mapping 为 @WebServlet("*.do")即响应所有以 .do 结尾的请求.在Servlet中通过反射,获取运行时类,之后invoke() 调用方法

 

  2.代码:test2.jsp ,  CustomerServlet2.java

 

  test2.jsp

 

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6 
7 测试2 8 9 10 11 Add212
13 14 Query215
16 17 Delete218
19 20 Update221
22 23 Edite224
25 26 27 28

 

 

  CustomerServlet2.java

 

1 package com.jason.mvcapp.servlet; 2  3 import java.io.IOException; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6  7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 13 /**14  * Servlet implementation class CustomerServlet215  */16 @WebServlet("*.do")17 public class CustomerServlet2 extends HttpServlet {18     private static final long serialVersionUID = 1L;19 20     protected void doGet(HttpServletRequest request,21             HttpServletResponse response) throws ServletException, IOException {22         doPost(request, response);23     }24 25     protected void doPost(HttpServletRequest request,26             HttpServletResponse response) throws ServletException, IOException {27         //1.获取servletPath:/add.do 或者 query.do28         String serveltPath = request.getServletPath();29         30          System.out.println(serveltPath);31         //2.去除/ 和 .do 得到对应的方法,如 add  query32         String methodName = serveltPath.substring(1);33         methodName = methodName.substring(0, methodName.length() - 3);34         // System.out.println(methodName);35 36         try {37             //3.利用反射获取methodName对应的方法38             Method method = getClass().getDeclaredMethod(methodName,39                     HttpServletRequest.class, HttpServletResponse.class);40             41             //4.利用反射调用方法42             method.invoke(this, request, response);43         } catch (Exception e) {44 45             e.printStackTrace();46         }47     }48 49     50     51     private void update(HttpServletRequest request, HttpServletResponse response)52             throws ServletException, IOException {53         System.out.println("update");54 55     }56 57     private void editeCustomer(HttpServletRequest request,58             HttpServletResponse response) throws ServletException, IOException {59         System.out.println("edit");60 61     }62 63     private void deleteCustomer(HttpServletRequest request,64             HttpServletResponse response) throws ServletException, IOException {65         System.out.println("delete");66 67     }68 69     private void query(HttpServletRequest request, HttpServletResponse response)70             throws ServletException, IOException {71         System.out.println("query");72 73     }74 75     private void addCustomer(HttpServletRequest request,76             HttpServletResponse response) throws ServletException, IOException {77         System.out.println("add");78     }79 80 }

 

 

总结:

  1)理解由方式一过度到方法二;

  2)理解反射获取当前类,获取提交的方法,及解析,调用相应的方法;

 

转载于:https://www.cnblogs.com/jasonHome/p/5528628.html

你可能感兴趣的文章
android permission
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
[Linux]PHP-FPM与NGINX的两种通讯方式
查看>>
Java实现二分查找
查看>>
架构图-模型
查看>>
黑马程序员_Java基础枚举类型
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
疯狂JAVA16课之对象与内存控制
查看>>
django ORM创建数据库方法
查看>>
php7 新特性整理
查看>>