zoukankan      html  css  js  c++  java
  • 一个servlet处理多个请求(使用Method的反射机制)

    方法一

    可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法。达到一个servlet处理多个请求的目的

    test.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'test.jsp' starting page</title>
    </head>
    <body>
    <!--
    请求的时候加上对应的请求参数
    -->
        <a href="customerServlet?method=add"> Add</a>
        <br>
        <br>
        <a href="customerServlet?method=query"> Query</a>
        <br>
        <br>
        <a href="customerServlet?method=delete"> Delete</a>
    </body>
    </html>
    

    CustomerServlet.Java:

    package com.aaa.mvcapp.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CustomerServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //获取对应的请求参数
            String method = request.getParameter("method");
            //根据请求参数去调用对应的方法。
            if ("add".equals(method)) {
                add(request, response);
    
            } else if ("query".equals(method)) {
                query(request, response);
            }
    
            else if ("delete".equals(method)) {
                delete(request, response);
            }
        }
    
        private void delete(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("delete");
    
        }
    
        private void query(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("query");
        }
    
        private void add(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("add");
    
        }
    }
    

    方法二

    1.在servlet的mapping中配置为 *.do来响应多个请求 
    2. 在servlet中获取servletPath。(例如/=add.do) 
    3. 去除/ 和.do,利用反射再去调用对应的方法。

    web.xml(servlet配置):

      <servlet>
        <servlet-name>CustomerServlet</servlet-name>
        <servlet-class>com.aaa.mvcapp.servlet.CustomerServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>CustomerServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>

    test.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'test.jsp' starting page</title>
    </head>
    <body>
        <a href="add.do"> Add</a>
        <br>
        <br>
        <a href="query.do"> Query</a>
        <br>
        <br>
        <a href="delete.do"> Delete</a>
    </body>
    </html>
    

    CustomerServlet.java:

    package com.aaa.mvcapp.servlet;
    
    import java.io.IOException;
    import java.lang.reflect.Method;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CustomerServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 获取方法名字
            String servletPath = request.getServletPath();// /xxxxxx.do
            // 去掉斜杠和.do
            String methodName = servletPath.substring(1, servletPath.length() - 3);// xxxxxx
    
            try {
                // 利用反射获取方法
                Method method = getClass().getDeclaredMethod(methodName,
                        HttpServletRequest.class, HttpServletResponse.class);
                // 执行相应的方法
                method.invoke(this, request, response);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    
        private void delete(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("delete");
    
        }
    
        private void query(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("query");
        }
    
        private void add(HttpServletRequest request, HttpServletResponse response) {
            // TODO Auto-generated method stub
            System.out.println("add");
    
        }
    }

    这样一来不同的请求可以会用同一个servlet来处理,在servlet内部为不同的请求分别有不同的方法去处理。

  • 相关阅读:
    《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)
    《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ
    《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
    Uva227.Puzzle
    UVa1587.Digit Counting
    《Two Days DIV + CSS》读书笔记——CSS选择器
    《Two Days DIV + CSS》读书笔记——CSS控制页面方式
    《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ
    《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅲ
    校赛总结
  • 原文地址:https://www.cnblogs.com/lxmyhappy/p/6878622.html
Copyright © 2011-2022 走看看