获取 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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
创建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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
结果:可以看到传入的数据
获取 POST 请求中的参数(2)
1.如果 POST 请求中的 body 是按照 JSON 的格式来传递, 那么通过 getParameter 就获取不到参数的值了!!!
类还是上面的PostParameter,但这里没有创建前端html文件,而是使用这个软件充当前端去发送请求;如上图所示,传入结果为123;
我们执行的结果却是:null
前端是把数据传给了后端的,但是后端拿不到数据
2.所以当POST 请求中的 body 是按照 JSON 的格式来传递,得使用 来获取
创建类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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
还是用postman 当前端
可以看到此时就可以得到数据(黄色框)了
但服务器拿到的 JSON 数据仍然是一个整体的 String 类型({“s”:123}), 如果要想获取到 s的具体值, 还需要搭配 JSON 库进一步解析.
获取 POST 请求中的参数(3)
引入 Jackson 这个库, 进行 JSON 解析.
- 在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>
- 1
- 2
- 3
- 4
- 5
如图演示:
当红色字体变成黑色字体,即导入成功;
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")); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
结果:
此时就直接得到了具体的参数值
对于 json 字符串和j对象的互相转换:
- 这里引用了lombok去简化student对象的属性设置:
1.在pom.xml中引入lombok库
2.在idea中安装lombok插件
@Dataclass 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 ); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.1将student对象转换成 json 字符串的结果:
2.2.将 json 字符串转换student对象的结果:
上面字符串转换对象时,使用的是Hashmap;因为那是时并没有新创建类,所以用了hashmap