JavaWeb:一个Servelt多个请求

时间:2022-02-20 14:51:46

一个Servelt多个请求

基础模拟

方法一:使用switch方法

 一、方法介绍

  • 方法:switch
  • 优点:方法简单,明了
  • 缺点:维护麻烦,保密性不好

二、代码实现

1、servlet类

package servlet;
/**
* 使用参数进行多个请求
*/ import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet("/CustomerServelet")
public class CustomerServelet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("method");
switch (method){
case "add":
add(request,response);
break;
case "update":
update(request,response);
break;
case "delet":
delet1(request,response);
break;
default:
break;
}
}

  //删除
private void delete(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delete");
}
  //更新
private void update(HttpServletRequest request, HttpServletResponse response) {
System.out.println("update");
}
  //添加
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("add");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}

2、jsp测试文件

<body>
  <a href="CustomerServelet?method=add">Add</a>
  <a href="CustomerServelet?method=update">Update</a>
  <a href="CustomerServelet?method=delete">Delete</a>
</body>

方法二:使用Java反射方法

 一、方法介绍

  • 方法:Java反射
  • 优点:保密性好,易于维护
  • 缺点:比较麻烦

二、代码实现

1、servlet类

package servlet;

import mvcdemo.CriteriaCustomer;
import mvcdemo.CustomerDAO;
import mvcentity.Customer;
import mvcimpl.CustomerDAOJdbcImpl; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List; @WebServlet("*.do")
//或者
//@WebServlet("*.do")//符合“*.do”的格式,则映射到里面。
public class CustomerServelet1 extends HttpServlet {
private static final long serialVersionUID=1L; private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到请求路径,格式为“/add.do”
String serveletPath=request.getServletPath();     //格式路径,获取方法名
String methodName=serveletPath.substring();//此时格式“*.do"
methodName=methodName.substring(,methodName.length()-);//此时格式"*"      //利用反射获取methodName对应的方法
try{
Method method=getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this,request,response);
}catch (Exception e){
e.printStackTrace();
} } //删除private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
System.out.println("delete");
}

  //更新
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
System.out.println("update");
} //增加private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
      System.out.println("add");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
}
}

2、jsp测试文件

<body>
<a href="add.do">Add</a>
<a href="update.do">Update</a>
<a href="delete.do">Delete</a>
</body>

三、说明

当运行jsp文件,并且点击其超链接,可以在控制台中显示出,相应的数据说明成功