本文实例讲述了JSP+jQuery使用Ajax方式调用JSON的实现方法,在这里分享给大家以供大家参考,具体的实现以及代码如下所示:
前台:
成都一家集口碑和实力的网站建设服务商,拥有专业的企业建站团队和靠谱的建站技术,10余年企业及个人网站建设经验 ,为成都上1000家客户提供网页设计制作,网站开发,企业网站制作建设等服务,包括成都营销型网站建设,品牌网站建设,同时也为不同行业的客户提供网站设计制作、网站建设的服务,包括成都电商型网站制作建设,装修行业网站制作建设,传统机械行业网站建设,传统农业行业网站制作建设。在成都做网站,选网站制作建设服务商就选成都创新互联。
submit
后台:
test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%
response.setContentType("application/x-json;charset=UTF-8");
String uid=request.getParameter("uid");
int ret=0;
String result = "{\"result\":" + ret + "}";
out.print(result);
%>
注意:返回json格式
如{"result":0}
需要设置 :
代码如下:
response.setContentType("application/x-json;charset=UTF-8");
jsp+jquery+ajax+json
Person、Address 2个类描述一个人,用ajax把数据取回显示在页面上,在DemoServlet里组装数据,项目用到的文件
json-lib-2.1-jkd15.jar
commons-lang-2.1.jar
commons-logging.jar
commons-collections-3.2.jar
commons-beanutils.jar
jquery.js
json.js
下面是详细代码:
Person:
public class Person implements java.io.Serializable{
private String name;
private String sex;
private Integer age;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address:
public class Address implements java.io.Serializable{
private String province;
private String city;
private String country;
public Address() {
}
public Address(String province, String city, String country){
this.province =province;
this.city = city;
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province =province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
DemoServlet:
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.JSONArray;
import net.sf.json.JSONObject;
import org.wncnke.json.pojo.Address;
import org.wncnke.json.pojo.Person;
public class DemoServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
try{
Personperson1 = new Person();
person1.setName("小王");
person1.setSex("女");
person1.setAge(23);
person1.setAddress(newAddress("辽宁省","大连市","高新园区"));
List list = new ArrayList();
list.add(person1);
try{
//取集合
JSONArray jsonArray = JSONArray.fromObject(list);
JSONObject jsonObject = new JSONObject();
jsonObject.put("person", jsonArray);
response.getWriter().write(jsonObject.toString());
} catch(IOException e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
web.xml:
json
index.jsp
DemoServlet
org.wncnke.json.servlet.DemoServlet
DemoServlet
/demoServlet
index.jsp:
view plaincopy to clipboardprint?
<%@ page language="java" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
Name
Sex
Age
Address
本文题目:创新互联jSON教程:JSP+jquery使用ajax方式调用json的实现方法
文章网址:http://www.shufengxianlan.com/qtweb/news19/63469.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联