• [原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析


    本博客为原创:综合 尚硅谷(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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>测试</title>
     8 </head>
     9 <body>
    10         
    11             <a href="customerServlet?method=add">Add1</a>
    12             <br><br>
    13         
    14             <a href="customerServlet?method=query">Query1</a>
    15             <br><br>
    16         
    17             <a href="customerServlet?method=delete">Delete1</a>
    18             <br><br>
    19             <br><br>
    20             <br><br>
    21             
    22             
    23         
    24 </body>
    25 </html>

      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 CustomerServlet
    12  */
    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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>测试2</title>
     8 </head>
     9 <body>
    10 
    11             <a href="addCustomer.do">Add2</a>
    12             <br><br>
    13         
    14             <a href="query.do">Query2</a>
    15             <br><br>
    16         
    17             <a href="deleteCustomer.do">Delete2</a>
    18             <br><br>
    19             
    20             <a href="update.do">Update2</a>
    21             <br><br>
    22             
    23             <a href="editeCustomer.do">Edite2</a>
    24             <br><br>
    25             
    26 
    27 </body>
    28 </html>

      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 CustomerServlet2
    15  */
    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.do
    28         String serveltPath = request.getServletPath();
    29         
    30          System.out.println(serveltPath);
    31         //2.去除/ 和 .do 得到对应的方法,如 add  query
    32         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)理解反射获取当前类,获取提交的方法,及解析,调用相应的方法;

  • 相关阅读:
    Comet OJ
    LOJ#2719. 「NOI2018」冒泡排序 DP+组合+树状数组
    LuoguP6747 『MdOI R3』Teleport 二进制+贪心
    LuoguP6748 『MdOI R3』Fallen Lord 树形DP+set
    LuoguP5576 [CmdOI2019]口头禅 后缀树+线段树+暴力
    LOJ#3161. 「NOI2019」I 君的探险 整体二分+随机化+二进制分组
    LOJ#2085. 「NOI2016」循环之美 莫比乌斯反演+杜教筛
    LuoguP5327 [ZJOI2019]语言 线段树合并+树链求并
    【考试题
    ELK
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5528628.html
Copyright © 2020-2023  润新知