用JSON格式提交数据到服务端

准备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 + "]";
	    }
}

submit.html文件




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

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);
    } 
} 

1. 获取浏览器提交的字符串
2. 把字符串转换为JSON对象

 

3. 把JSON对象转换为Hero对象

 

最后配置web.xml



 
    
        SubmitServlet
        SubmitServlet
    
 
    
        SubmitServlet
        /submitServlet
    
 

启动tomcat访问http://127.0.0.1:8080/项目名/submit.html

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

 

 

获取一个对象

准备GetOneServlet

import java.io.IOException;
import java.io.Writer;
 
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 GetOneServlet extends HttpServlet { 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
          
        Hero hero = new Hero();
        hero.setName("盖伦");
        hero.setHp(353);
         
        JSONObject json= new JSONObject();
   
        json.put("hero", JSONObject.fromObject(hero));
        response.setContentType("text/html;charset=utf-8"); 
        response.getWriter().print(json);
    } 
} 

web.xml



 
    
        SubmitServlet
        SubmitServlet
    
 
    
        SubmitServlet
        /submitServlet
    
    
        GetOneServlet
        GetOneServlet
    
 
    
        GetOneServlet
        /getOneServlet
    
 

getOne.html




 
用AJAX以JSON方式获取数据 
 
 
 
      
   
    

测试(重启tomcat)

启动tomcat访问http://127.0.0.1:8080/项目名/getOne.html

 

3 获取多个对象

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import net.sf.json.JSONSerializer; 
   
public class GetManyServlet extends HttpServlet { 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        List heros = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Hero hero = new Hero();
            hero.setName("name"+i);
            hero.setHp(500+i);
            heros.add(hero);
        }
         
        String result =JSONSerializer.toJSON(heros).toString();
 
        response.setContentType("text/html;charset=utf-8"); 
        response.getWriter().print(result);
    } 
    public static void main(String[] args) {
        List heros = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Hero hero = new Hero();
            hero.setName("name"+i);
            hero.setHp(500+i);
            heros.add(hero);
        }
         
        System.out.println(JSONSerializer.toJSON(heros).toString());
    }
} 

web.xml



 
    
        SubmitServlet
        SubmitServlet
    
 
    
        SubmitServlet
        /submitServlet
    
    
        GetOneServlet
        GetOneServlet
    
 
    
        GetOneServlet
        /getOneServlet
    
    
        GetManyServlet
        GetManyServlet
    
 
    
        GetManyServlet
        /getManyServlet
    
 

getMany.html




 
用AJAX以JSON方式获取数据 
 
 
 
      
   
    

 

测试(重启tomcat)

启动tomcat访问http://127.0.0.1:8080/项目名/getMany.html

用JSON格式提交数据到服务端_第1张图片

 

 

 

 

你可能感兴趣的:(JavaEE)