欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用JSON格式提交数据到服务端的实例代码

程序员文章站 2022-05-01 17:14:01
准备hero.java public class hero { private string name; private int hp; pub...

准备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访问项目名/submit.html

使用JSON格式提交数据到服务端的实例代码

在tomcat控制台看到传来的数据

使用JSON格式提交数据到服务端的实例代码

总结

以上所述是小编给大家介绍的使用json格式提交数据到服务端的实例代码,希望对大家有所帮助