servlet如何获取PUT和DELETE请求的参数

1. servlet为何不能获取PUT和DELETE请求的参数

Servlet的规范是POST的数据需要转给request.getParameter*()方法,没有规定PUT和DELETE请求也这么做

The Servlet spec requires form data to be available for HTTP POST but not for HTTP PUT or PATCH requests. This filter intercepts HTTP PUT and PATCH requests where content type is 'application/x-www-form-urlencoded', reads form encoded content from the body of the request, and wraps the ServletRequest in order to make the form data available as request parameters just like it is for HTTP POST requests.

2. 解决方案

        2-1  前端使用ajax请求时,发送json类型的参数

        2-2  后端使用字符流读取前端传递的参数

3. 具体的实现

        3-1 前端部分

$(function () {
  // 1. 请求参数
  let params = {
    id: 51,
    phone: '17911113333',
    userCode: 'barrss',
    userName: 'barrss',
    address: '不详',
    userRole: 1,
    gender: 1,
    birthday: '1998-10-12',
  }
  // 2. 使用jQuery的$.ajax()发送put类型请求进行用户修改
  $.ajax({
    url: 'http://localhost/day81/updateUser',
    // 请求类型是put
    type: 'put',
    dataType: 'json',
    // 通过请求头告诉服务端,发送的数据是json类型
    contentType: 'application/json',
    // 前端必须传递json字符串,否则后端无法解析
    data: JSON.stringify(params),
    headers: { token: '***.***zAyMjYwfQ.***' },
    success(res) {
      console.log(res)
    },
    error(e) {
      console.log(e)
    },
  })
})

        3-2 后端MyUtil工具类中定义方法 getJSONParams

/**
   *  获得前端发送的json类型参数
   *
   * @param req 封装客户端请求信息的对象
   * @return 包含请求参数的Map集合
   */
  public static Map<String, String> getJSONParams(HttpServletRequest req) throws ServletException, IOException {
    // 1. 打开输入流读取客户端写入的json字符串
    InputStream in = req.getInputStream();
    // 2. 使用BufferedReader包装流,指定字符串
    BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
    String str = null;
    StringBuilder builder = new StringBuilder();
    // 3. 读取客户端传递的json字符串参数,拼接到StringBuilder中
    while ((str = reader.readLine()) != null) {
      builder.append(str);
    }
    String params = builder.toString();
    // 4. 去除json字符串中的{}
    params = params.substring(1, params.length() - 1);
    // 5. 去除json字符串中的 "
    params = params.replace("\"", "");
    Map<String, String> map = new HashMap<>();
    // 6. 拆分字符串,把key=value键值对, 存放到map中
    String[] arr = params.split(",");
    for (String item : arr) {
      String[] code = item.split(":");
      map.put(code[0], code[1]);
    }
    return map;
  }

      3-3 servlet中的doPut方法中使用getJSONParams即可

  @Override
  protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Map<String, String> map = MyUtil.getJSONParams(req);
    req.setAttribute("params", map);
    doPost(req, res);
  }

4. 总结

        使用此种方法获取客户端的put和delete请求方法,要规定客户端必须传递json字符串,后端需要自己开发方法来进行获取,使用起来并不方便,也不灵活。可以考虑使用POST代替PUT或DELETE请求,方便获取参数,不仅安全,而且高效!不妥之处还请指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值