博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ajax与Servlet
阅读量:6037 次
发布时间:2019-06-20

本文共 4370 字,大约阅读时间需要 14 分钟。

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
用户名:
前台jsp页面

 

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);            }}
创建对应的servlet

 

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 + "]";    }}
Student实体类

 

<%@ 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
用户名:
前台jsp页面

 

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);    }}
对应的servlet

 

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
用户名:
前台jsp页面

 

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");        ArrayList
list = 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); }}
对应的servlet

 

转载于:https://www.cnblogs.com/xtdxs/p/7093639.html

你可能感兴趣的文章
[Debug] Use Remote Sources to Debug a Web App on an Emulator, Simulator, or Physical Device
查看>>
MySQL读写分离
查看>>
js&jquery 获取select下拉框的值、文本内容、自定义属性
查看>>
搭建ssm框架项目基本原理和主要的配置文件小结
查看>>
导出表结构sql语句
查看>>
centOS7服务管理与启动流程
查看>>
Unity2018.1中文更新日志速览版
查看>>
WPF 4 日历控件(Calendar)
查看>>
树莓派之OLED12864视频播放—BadApple
查看>>
论如何优雅地拿下PHPCMS
查看>>
[PHP] 数据结构-二叉树的创建PHP实现
查看>>
让你的Blend“编辑其他模板”菜单里出现你的Style
查看>>
UILabel添加图片之富文本的简单应用
查看>>
Ipython Notebook ipynb文件转化为Python脚本
查看>>
springboot~rabbitmq的队列初始化和绑定
查看>>
【混淆矩阵】完整版
查看>>
jboss-as- 7.1.1.Final配置jndi数据源
查看>>
JavaScript:数据类型
查看>>
POI之Excel导出
查看>>
yii2 緩存
查看>>