How to submit data in JSON format to the server

php中世界最好的语言
Release: 2018-04-08 15:32:35
Original
1789 people have browsed it

This time I bring to you, what are theprecautionswhen submitting JSON format data to the server. The following is a practical case, let's take a look.

Prepare Hero.java

public class Hero { private String name; private int hp; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } @Override public String toString() { return "Hero [name=" + name + ", hp=" + hp + "]"; } } public class Hero { private String name; private int hp; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } @Override public String toString() { return "Hero [name=" + name + ", hp=" + hp + "]"; } }submit.html文件 [html] view plain copy print?    用AJAX以JSON方式提交数据  
名称:
血量:

用AJAX以JSON方式提交数据
名称:
血量:

Copy after login

The function of JSON.stringify function is to convert ajavascript objectinto astringin JSON format.

Prepare SubmitServlet to receive data

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; public class SubmitServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data =request.getParameter("data"); System.out.println("服务端接收到的数据是:" +data); JSONObject json=JSONObject.fromObject(data); System.out.println("转换为JSON对象之后是:"+ json); Hero hero = (Hero)JSONObject.toBean(json,Hero.class); System.out.println("转换为Hero对象之后是:"+hero); } } import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; public class SubmitServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data =request.getParameter("data"); System.out.println("服务端接收到的数据是:" +data); JSONObject json=JSONObject.fromObject(data); System.out.println("转换为JSON对象之后是:"+ json); Hero hero = (Hero)JSONObject.toBean(json,Hero.class); System.out.println("转换为Hero对象之后是:"+hero); } }
Copy after login

1. Get the string submitted by the browser

2. Convert the string into a JSON object

3 . Convert JSON object to Hero object

Finally configure web.xml

   SubmitServlet SubmitServlet   SubmitServlet /submitServlet      SubmitServlet SubmitServlet   SubmitServlet /submitServlet 
Copy after login

Start tomcat to access http://127.0.0.1:8080/project name/submit .html

#See the data coming from the tomcat console

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

What is the difference between wx:for and wx:for-item in WeChat mini program

Implementation JS date and time picker

How does the Angular parent component call the child component

The above is the detailed content of How to submit data in JSON format to the server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!