1.后台返回text类型的数据
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>My JSP 'index.jsp' starting page 用户名:
public class AjaxServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("进入了ajax.........."); response.setHeader("Content-type", "text/html;charset=utf-8"); // 01.获取ajax请求过来的name值 String name = request.getParameter("name"); response.getWriter().print(name); }}
2.返回单个对象
public class Student { private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public Student(String name, String pwd) { super(); this.name = name; this.pwd = pwd; } public Student() { super(); } @Override public String toString() { return "Student [name=" + name + ", pwd=" + pwd + "]"; }}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>My JSP 'index.jsp' starting page 用户名:
public class AjaxServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("进入了ajax.........."); response.setHeader("Content-type", "text/html;charset=utf-8"); // 创建一个Student对象 返回给前台 Student student = new Student("admin1", "123456"); // 需要把student对象转换成json格式 System.out.println("转换前==》" + student); Gson gson = new Gson(); // json 就是转换之后的 student对象 {"name":"admin","pwd":"123456"} String json = gson.toJson(student); System.out.println("转换后==" + json); response.getWriter().print(json); }}
3.返回对象的集合
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>My JSP 'index.jsp' starting page 用户名:
public class AjaxServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("进入了ajax.........."); response.setHeader("Content-type", "text/html;charset=utf-8"); Student student1 = new Student("admin1", "123456"); Student student2 = new Student("admin2", "123456"); Student student3 = new Student("admin3", "123456"); Student student4 = new Student("admin4", "123456"); ArrayListlist = new ArrayList (); list.add(student1); list.add(student2); list.add(student3); list.add(student4); System.out.println("转换前==》" + list); Gson gson = new Gson(); String json = gson.toJson(list); System.out.println(json); response.getWriter().print(json); }}