秋天,那永远是蓝湛湛的天空,会突然翻脸而露出险恶的颜色,热带台风夹着密云暴雨,洪水潜流着,复苏的草原又泛起点点苍苍的颜色。然而,台风暴雨一闪而过,强烈的气流依然抖动着耀眼的波光。这时,只有北来的候鸟知道这张温暖的床眠,那飞翔的天鹅、鸿雁和野鸭,就像一片阴深的云朵,使这儿显得更苍郁了。
准备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?<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用AJAX以JSON方式提交数据</title> <script type="text/javascript" src="jquery.min.js"></script> </head> <body> <form > 名称:<input type="text" id="name"/><br/> 血量:<input type="text" id="hp"/><br/> <input type="button" value="提交" id="sender"> </form> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var name=document.getElementById('name').value; var hp=document.getElementById('hp').value; var hero={"name":name,"hp":hp}; var url="submitServlet"; $.post( url, {"data":JSON.stringify(hero)}, function(data) { alert("提交成功,请在Tomcat控制台查看服务端接收到的数据"); }); }); </script> </body> </body> </html> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用AJAX以JSON方式提交数据</title> <script type="text/javascript" src="jquery.min.js"></script> </head> <body> <form > 名称:<input type="text" id="name"/><br/> 血量:<input type="text" id="hp"/><br/> <input type="button" value="提交" id="sender"> </form> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var name=document.getElementById('name').value; var hp=document.getElementById('hp').value; var hero={"name":name,"hp":hp}; var url="submitServlet"; $.post( url, {"data":JSON.stringify(hero)}, function(data) { alert("提交成功,请在Tomcat控制台查看服务端接收到的数据"); }); }); </script> </body> </body> </html>
JSON.stringify函数的作用是将一个javascript对象,转换为JSON格式的字符串。
准备SubmitServlet用来接收数据
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); } }
1. 获取浏览器提交的字符串
2. 把字符串转换为JSON对象
3. 把JSON对象转换为Hero对象
最后配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>SubmitServlet</servlet-name> <servlet-class>SubmitServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SubmitServlet</servlet-name> <url-pattern>/submitServlet</url-pattern> </servlet-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>SubmitServlet</servlet-name> <servlet-class>SubmitServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SubmitServlet</servlet-name> <url-pattern>/submitServlet</url-pattern> </servlet-mapping>
</web-app>启动tomcat访问http://127.0.0.1:8080/项目名/submit.html
在tomcat控制台看到传来的数据
总结
以上所述是小编给大家介绍的使用JSON格式提交数据到服务端的实例代码,希望对大家有所帮助,如果大家有任何疑问花园给我留言,小编会及时回复大家的!
以上就是使用JSON格式提交数据到服务端的实例代码。什么叫做不简单?能够把简单的事情天天做好,就是不简单;什么叫做不容易?大家公认的、非常容易的事情。非常认真地做好它,就是不容易。更多关于使用JSON格式提交数据到服务端的实例代码请关注haodaima.com其它相关文章!