我们在对JavaWEB工程进行开发的时候,我们经常会遇到这样一个问题,在jsp中发送到Servlet的每一个请求都要写一个对应的Servlet,这样会造成一个工程完成下来需要写几十个Servlet,那么怎么可以做到多个请求共用一个Servlet呢?

    下面介绍两种方法(第一种比较难理解一点,第二种相对容易):

    一、利用反射原理(根据获取到的url-pattern,截取出相应的方法名,进而调用相应的方法)

           1.配置web.xml文件,<url-pattern>中设置以 *.扩展名 的方式进行匹配(下面的例子以 *.do 匹配,调用的Servlet为 StaffServlet,对Staff表的查询query 和删除delete 请求进行处理)

  1.  
    <servlet>
  2.  
    <servlet-name>StaffServlet</servlet-name>
  3.  
    <servlet-class>cn.edu.lingnan.servlet.StaffServlet</servlet-class>
  4.  
    </servlet>
  5.  
    <servlet-mapping>
  6.  
    <servlet-name>StaffServlet</servlet-name>
  7.  
    <url-pattern>*.do</url-pattern>
  8.  
    </servlet-mapping>

           所有以 .do 结尾的请求都会调用StaffServlet

            2.编写Servlet(例子:StaffServlet)

  1.  
    public class StaffServlet extends HttpServlet {
  2.  
    private static final long serialVersionUID = 1L;
  3.  
     
  4.  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5.  
    doPost(request, response);
  6.  
    }
  7.  
     
  8.  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  9.  
    //1.获取能够与“url-pattern”中匹配的路径
  10.  
    String method = request.getServletPath();
  11.  
     
  12.  
    //(此时处理的请求是查询 query.do)
  13.  
    System.out.println("request.getServletPath()获取的值为: " + method);//输出 /query.do
  14.  
     
  15.  
    //2.通过字符串截取,把方法名 query 截取出来
  16.  
    method = method.substring(1, method.length()-3);
  17.  
     
  18.  
    System.out.println("截取后的值为: "+ method);
  19.  
     
  20.  
    Method m = null;
  21.  
    try {
  22.  
    //3.获取当前类中名字为 method 的方法
  23.  
    m = this.getClass().getDeclaredMethod(method,
  24.  
    HttpServletRequest.class, HttpServletResponse.class);
  25.  
     
  26.  
    //4.调用 method 方法
  27.  
    m.invoke(this, request, response);
  28.  
    } catch (Exception e) {
  29.  
    e.printStackTrace();
  30.  
    }
  31.  
    }
  32.  
     
  33.  
    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34.  
    System.out.println("query方法被调用");
  35.  
     
  36.  
    }
  37.  
     
  38.  
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  39.  
    System.out.println("delete方法被调用");
  40.  
     
  41.  
    }
  42.  
    }

 

            3. jsp 中发送请求调用servlet

 

  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>Insert title here</title>
  8.  
    </head>
  9.  
    <body>
  10.  
    <a href="query.do">这是一个查询请求,调用Servlet中的query方法</a>
  11.  
    <br>
  12.  
    <br>
  13.  
    <a href="delete.do">这是一个删除请求,调用Servlet中的delete方法</a>
  14.  
    </body>
  15.  
    </html>

               点查询和删除的超链接后台输出结果如下:

                如还需要添加其他操作请求,只需在Servlet中添加相应名字的方法,如更新请求 jsp中 update.do,  servlet中添加如下方法:

  1.  
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2.  
     
  3.  
    }

                其他请求也是如上操作即可,注意:url-pattern 中的扩展名可以是任意字符,不一定要是 .do , 例如也可以是 .doStaff , 但是改了扩展名了要注意修改截取的字符串的位置,如修改由.do 修改为 .doStaff 时,doPost中

                    method = method.substring(1, method.length()-3);需修改为(.do 为三个字符)

                    method = method.substring(1, method.length()-8);(.doStaff 为八个字符)

                    jsp 中调用时,则也相应修改 query.do ---> query.doStaff

 

    二、利用URL中的参数传送需要调用的方法名

            1.配置web.xml, 映射Servlet路径(以下以 StudentServlet 调用 Servlet 中的 query 和 delete 方法为例子)

  1.  
    <servlet>
  2.  
    <servlet-name>StudentServlet</servlet-name>
  3.  
    <servlet-class>servlet.StudentServlet</servlet-class>
  4.  
    </servlet>
  5.  
    <servlet-mapping>
  6.  
    <servlet-name>StudentServlet</servlet-name>
  7.  
    <url-pattern>/studentServlet</url-pattern>
  8.  
    </servlet-mapping>

            2.编写Servlet(StudentServlet)

 

  1.  
    package 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 StudentServlet
  12.  
    */
  13.  
     
  14.  
    public class StudentServlet extends HttpServlet {
  15.  
    private static final long serialVersionUID = 1L;
  16.  
     
  17.  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18.  
    this.doPost(request, response);
  19.  
    }
  20.  
     
  21.  
     
  22.  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  23.  
    //获取请求参数 method
  24.  
    String method = request.getParameter("method");
  25.  
     
  26.  
    System.out.println("获取到的method参数为: " + method);
  27.  
    //调用 method 方法
  28.  
    if(method.equals("query")) {
  29.  
    this.query(request, response);
  30.  
    }else {
  31.  
    if(method.equals("delete")) {
  32.  
    this.delete(request, response);
  33.  
    }
  34.  
    }
  35.  
    }
  36.  
     
  37.  
    private void query(HttpServletRequest request, HttpServletResponse response)
  38.  
    throws ServletException, IOException {
  39.  
    System.out.println("query 方法被调用");
  40.  
    }
  41.  
     
  42.  
    private void delete(HttpServletRequest request, HttpServletResponse response)
  43.  
    throws ServletException, IOException {
  44.  
    System.out.println("delete 方法被调用");
  45.  
    }
  46.  
    }

               3. jsp中发送请求调用Servlet

  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>Insert title here</title>
  8.  
    </head>
  9.  
    <body>
  10.  
    <a href="studentServlet?method=query">这是一个查询请求,调用Servlet中的query方法</a>
  11.  
    <br>
  12.  
    <br>
  13.  
    <a href="delete.do?method=delete">这是一个删除请求,调用Servlet中的delete方法</a>
  14.  
    </body>
  15.  
    </html>

                点查询和删除超链接后台输出结果如下:

        

 

posted on 2019-08-13 11:53  itprobie-菜鸟程序员  阅读(1329)  评论(0编辑  收藏  举报