网络知识 娱乐 获取post中的请求参数1

获取post中的请求参数1

获取 POST 请求中的参数(1)

POST 请求的参数一般通过 body 传递给服务器. body 中的数据格式有很多种.
如果是采用 form 表单的形式, 可以通过 getParameter 获取参数的值.
在这里插入图片描述

创建类PostParameter


//post通过body传参(配和post_text.html)
@WebServlet("/postparameter")
public class PostParameter extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //防止返回的结果乱码
        resp.setContentType("text/html;charSet=utf-8");
        //得到请求参数的值
        String post = req.getParameter("s");
        //返回结果
        resp.getWriter().println("post传参结果:" + post);



    }
}

创建post_text.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用post——get得参数</title>
</head>
<body>

<form action="postparameter" method="post">
    <div style="margin-top:50px;margin-left:40%;">
        <h1 style="padding-left:50px;">post传参</h1>
        参数:<input type="text" name="s">
        <input type="submit" value=" 提 交 ">

    </div>
</form>

</body>
</html>

在这里插入图片描述
结果:可以看到传入的数据
在这里插入图片描述

获取 POST 请求中的参数(2)

1.如果 POST 请求中的 body 是按照 JSON 的格式来传递, 那么通过 getParameter 就获取不到参数的值了!!!
在这里插入图片描述
类还是上面的PostParameter,但这里没有创建前端html文件,而是使用postman这个软件充当前端去发送请求;如上图所示,传入结果为123;

我们执行的结果却是:null

前端是把数据传给了后端的,但是后端拿不到数据

2.所以当POST 请求中的 body 是按照 JSON 的格式来传递,得使用 InputStream 来获取

创建类PostparameterJson

@WebServlet("/PostparameterJson")
public class PostparameterJson extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置返回值类型和编码
        resp.setContentType("text/html;charSet=utf-8");
        //1.得到数据流
        ServletInputStream inputStream=req.getInputStream();
        //2.找一个容器用来存储流
        byte [] bytes=new byte[req.getContentLength()];
        inputStream.read(bytes);
        //3.将数组转换成字符串
        String s=new String(bytes,"utf-8");
        //4.返回结果
        resp.getWriter().println("post得到的参数:" +s);

    }
}

还是用postman 当前端
在这里插入图片描述
可以看到此时就可以得到数据(黄色框)了
但服务器拿到的 JSON 数据仍然是一个整体的 String 类型({“s”:123}), 如果要想获取到 s的具体值, 还需要搭配 JSON 库进一步解析.

获取 POST 请求中的参数(3)

引入 Jackson 这个库, 进行 JSON 解析.

  1. 在https://mvnrepository.com/中央仓库中搜索 Jackson, 选择 JackSon Databind
    在这里插入图片描述
    2.把中央仓库中的依赖配置添加到 pom.xml 中:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.3</version>
</dependency>

如图演示:
在这里插入图片描述
当红色字体变成黑色字体,即导入成功;

3.在 PostParameterJson 类中修改代码
增加了注释5和6(json字符串转对象)

@WebServlet("/PostparameterJson")
public class PostparameterJson extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置返回值类型和编码
        resp.setContentType("text/html;charSet=utf-8");
        //1.得到数据流
        ServletInputStream inputStream=req.getInputStream();
        //2.找一个容器用来存储流
        byte [] bytes=new byte[req.getContentLength()];
        inputStream.read(bytes);
        //3.将数组转换成字符串或者是对象(这里是转成字符串)
        String s=new String(bytes,"utf-8");
        //4.返回结果1
        resp.getWriter().println("post得到的参数:" +s);
        // 5.字符串转换成对象(或字典)
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, String> map = objectMapper.readValue(s, HashMap.class);
        //6.返回结果2
        resp.getWriter().println("导入 Jackson后post得到的参数:" + map.get("s"));

    }
}

结果:
在这里插入图片描述
此时就直接得到了具体的参数值

对于 json 字符串和j对象的互相转换:

  • 这里引用了lombok去简化student对象的属性设置:
    1.在pom.xml中引入lombok库
    2.在idea中安装lombok插件
@Data

class Student{
    String id;
    String name;
    String password;
}

public class Json_String_Object {
    public static void main(String[] args) throws JsonProcessingException {
        //1.创建一个 json 操作对象
        ObjectMapper objectMapper=new ObjectMapper();
        //2.1将student对象转换成 json 字符串
        Student student = new Student();
        student.setId("1");
        student.setName("Java");
        student.setPassword("123");
        String result = objectMapper.writeValueAsString(student);
        System.out.println(result);

       //2.2.将 json 字符串转换对象
        String jsonStr = "{"id":2,"name":"lisi","password":"456"}";
        Student lisi = objectMapper.readValue(jsonStr, Student.class);
        //输出整个对象
        System.out.println(lisi);
        //只输出对象的某个参数值
        System.out.println("id:"+lisi.id );
    }

}

2.1将student对象转换成 json 字符串的结果:
在这里插入图片描述
2.2.将 json 字符串转换student对象的结果:
在这里插入图片描述
上面字符串转换对象时,使用的是Hashmap;因为那是时并没有新创建类,所以用了hashmap