如何多个请求运用一个servlet

方法一  jsp部分页面为 :

     Add
 
    Query

    Delete

    Update
        servlet  配置 :

    
    CustomerServlet1
    com.zyl.mvcapp.servlet.CustomerServlet
   

 
    CustomerServlet1
    /customerServlet  
 

      servlet部分代码:

       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request, response);
      }
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        switch (method) {
            case "add":add(request,response);break;
            case "query":query(request,response);break;
            case "delete":delete(request,response);break;
            case "update":update(request,response);break;
        }
    }

  private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("update");
        
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("delete");
        
    }
    
    private void query(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("query");
        
    }
    private void add(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add");
    }
    private void edit(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("edit"); 
    }

     缺陷方法一:不安全在浏览器会显示操作的方法;且需要调加一种方法时需要大改

方法二  jsp部分页面为 :

       Add
     


     Query
   


    Delete
   


    Update
   


    Edit

        servlet  配置 :


    CustomerServlet1
    com.zyl.mvcapp.servlet.CustomerServlet
 

 
    CustomerServlet1
    *.do
 

servlet部分代码: 

       

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request, response);
      }
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodname = servletPath.substring(1);
        methodname = methodname.substring(0,methodname.length()-3);
        Method method;
        try {
            method = getClass().getDeclaredMethod(methodname, HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this, request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//更新方法

  private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("update");
        
    }

//删除方法
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("delete");
        
    }
    // 查询方法
    private void query(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("query");
        
    }
    //添加方法
    private void add(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add");
    }

//编辑方法
    private void edit(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("edit"); 
    }

你可能感兴趣的:(Java)