ajax与Servlet
1.后台返回text类型的数据
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
//获取用户的输入
var name= $("#name").val();
$.ajax({
url:"AjaxServlet", /*对应的是web.xml文件中url 也是我们的请求路径 */
type:"post", /* 请求的方式 */
data:"name="+name, /* 请求中携带的数据 */
dataType:"text", /* 后台返回的数据类型 */
beforeSend:function(){
alert("请求正在处理。。。。。。");
},
success:function(data){
alert(data); }
});
});
});
</script> </head> <body> 用户名:<input type="text" id="name">
<input type="button" id="btn" value="请求ajax"> </body>
</html>
前台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 + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
//获取用户的输入
var name= $("#name").val();
$.ajax({
url:"AjaxServlet", /*对应的是web.xml文件中url 也是我们的请求路径 */
type:"post", /* 请求的方式 */
data:"name="+name, /* 请求中携带的数据 */
dataType:"json", /* 后台返回的数据类型 */
beforeSend:function(){
alert("请求正在处理。。。。。。");
},
success:function(data){
/* 返回集合 */ //返回单个对象 alert(data);
$("#myDiv").append("姓名:"+data.name);
$("#myDiv").append("密码:"+data.pwd);
}
});
});
});
</script> </head> <body> 用户名:<input type="text" id="name">
<input type="button" id="btn" value="请求ajax">
<div id="myDiv"></div> </body>
</html>
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 + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
//获取用户的输入
var name= $("#name").val();
$.ajax({
url:"AjaxServlet", /*对应的是web.xml文件中url 也是我们的请求路径 */
type:"post", /* 请求的方式 */
data:"name="+name, /* 请求中携带的数据 */
dataType:"json", /* 后台返回的数据类型 */
beforeSend:function(){
alert("请求正在处理。。。。。。");
},
success:function(data){
/* 返回集合 */
$("#myDiv").append("<span>姓名</span> ");
$("#myDiv").append("<span>密码</span></br>");
//遍历传递过来的json数组
$(data).each(function(i){
$("#myDiv").append("<span>"+data[i].name+"</span> ");
$("#myDiv").append("<span>"+data[i].pwd+"</span></br>");
}) }
});
});
});
</script> </head> <body> 用户名:<input type="text" id="name">
<input type="button" id="btn" value="请求ajax">
<div id="myDiv"></div> </body>
</html>
前台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<Student> list = new ArrayList<Student>();
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
ajax与Servlet的更多相关文章
- ajax和servlet交互,表单日历插件,表单验证,form.js
我的index.jsp <body> <a>点我获取数据</a> <table border=1px> <tr> <td>ID& ...
- ajax和servlet交互
网上有比较多的教程来将如何实现ajax与servlet的交互了,这里和这里的教程可以参考参考,在此处我只简单说明一下,并记录一下我这次遇到的问题. 整个思路是:写个js函数,在里面使用XHR(ajax ...
- Jquery+ajax+json+servlet原理和Demo
Jquery+ajax+json+servlet原理和Demo 大致过程: 用户时间点击,触发js,设置$.ajax,开始请求.服务器响应,获取ajax传递的值,然后处理.以JSON格式返回给ajax ...
- Ajax+Jsp+servlet+json技术的使用
Ajax+Jsp+servlet+json技术的使用 在使用json的时候,记得必须导入如下几个.jar包,最好是手动复制.jar包只lib路径下,否则可能出现异常. commons-beanutil ...
- servlet向ajax传递list数据类型,ajax向servlet传递array数据类型
因工作需要, 1,后台向前台传递一个list 2,前台向后台传递类似于list的结构,但是因为javascript不支持list类型,所以只能使用二维数组代替 后台运行后的截图: ...
- ajax调用servlet
1.利用myecilpse建立一个web项目 2.导入需要的包: commons-beanutils.jar commons-collections-3.1.jar commons-lan ...
- 服务器端AJAX的Servlet代码实现
package com.itheima.servlet; import java.io.IOException; import javax.servlet.ServletException; impo ...
- Ajax(6) Ajax向servlet请求数据库操作 并显示到当前页面 这个未经测试
假设:1.你的页面在Web-Root下,内容为: <div id="showMsg"></div><input type="text&quo ...
- ajax与servlet交互(通过JSON),JAVA的arraylist传到前端的方法
所实现的效果:首先从前端(ajax)传参数给servlet,然后servlet经过处理,把arraylist类型的参数以JSON字符串的形式返回给前端(ajax),然后前端经过解析,把JSON字符串解 ...
随机推荐
- underscorejs-indexBy学习
2.19 indexBy 2.19.1 语法 _.indexBy(list, iteratee, [context]) 2.19.2 说明 给定一个list,和 一个用来返回一个在列表中的每个元素键 ...
- DCI
理论: 某个data,在一个特定的场景中,以某个角色role,来与该场景中的其他角色进行交互.这个过程要以代码的方式表达出来,他要求data本身不具备交互行为, 有交互行为的是角色,当一个data没有 ...
- 查看 yum 安装软件包的路径
yum:列出已安装的安装包 [root@localhost ~]# yum list | grep mysql akonadi-mysql.x86_64 1.9.2-4.el7 base apr-ut ...
- iOS常用动画-b
CoreAnimationEffect.h // CoreAnimationEffect // // Created by VincentXue on 13-1-19. // Copyright ...
- 转:Hprose for php(二)——服务器
文章来自于:http://blog.csdn.net/half1/article/details/21252879 本文将介绍Hprose for php服务器的更多细节 1.发布服务 Hprose提 ...
- 使用Thumbnails对一个文件夹下的所有图片进行压缩处理
public static void compressPic(){ try { Thumbnails.of(new File("/home/y/my_temp/ydbg-xy-pic&quo ...
- gcc编译动态和静态链接库
我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而是 ...
- 7.5 Point-in-Time (Incremental) Recovery Using the Binary Log 使用binay log 基于时间点恢复
7.5 Point-in-Time (Incremental) Recovery Using the Binary Log 使用binay log 基于时间点恢复 7.5.1 Point-in-Tim ...
- VirtualBox - 自动调整屏幕大小,显示分辨率
在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕调整不太好,操作起来非常不方便,需要安装Vbox的增强功能.具体如下:1, 在 设备--> 安装增强功能这时会自动加载VBOXA ...
- html幻灯效果页面
方式一: <!DOCTYPE HTML> <html> <head> <style> #cont { position: relative; heigh ...