您好,登錄后才能下訂單哦!
準備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格式提交數據到服務端的實例代碼,希望對大家有所幫助,如果大家有任何疑問花園給我留言,小編會及時回復大家的!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。