当前位置 主页 > 服务器问题 > win服务器问题汇总 > 最大化 缩小

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

    栏目:win服务器问题汇总 时间:2019-11-10 08:59

    准备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" /><br/> 
      血量:<input type="text" /><br/> 
      <input type="button" value="提交" >  
     </form> 
     <div ></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" /><br/> 
      血量:<input type="text" /><br/> 
      <input type="button" value="提交" > 
     </form> 
     <div ></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);
     } 
    }
    
    下一篇:没有了