Users
package bean;
public class Users {
private Integer uid;
private String username;
private String password;
private Double money;
public Users() {
}
public Users(Integer uid, String username, String password, Double money) {
this.uid = uid;
this.username = username;
this.password = password;
this.money = money;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Users{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", money=" + money +
'}';
}
}
GetUSersServlet
package servlet;
import bean.Users;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/getuser")
public class GetUSersServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收参数
String uid = req.getParameter("uid");
int userid = Integer.parseInt(uid);
Users users = null;
switch (userid){
case 1:
users=new Users(1,"张三1","abc1",11.11);
break;
case 2:
users=new Users(2,"张三2","abc2",11.22);
break;
case 3:
users=new Users(3,"张三3","abc3",11.33);
break;
case 4:
users=new Users(4,"张三4","abc4",11.44);
break;
default:
users=new Users();
}
//java->json
JSONObject jsonObject = JSONObject.fromObject(users);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println(jsonObject);
}
}
TestServlet
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/testuname")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收参数
String username = req.getParameter("username");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if("admin".equals(username)){
writer.println("账户已被注册");
}else {
writer.println("账户可用");
}
}
}
Demo
package test;
import bean.Users;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
//java->json
Users users = new Users(11,"aaa","123456",123.456);
JSONObject jsonObject = JSONObject.fromObject(users);
System.out.println(jsonObject);//json
//json->java
String str="{\"money\":123.456,\"password\":\"123456\",\"uid\":11,\"username\":\"aaa\"}";
JSONObject jsonObject1 = JSONObject.fromObject(str);
Object o = JSONObject.toBean(jsonObject1, Users.class);
System.out.println("o="+o);
//java集合->json
ArrayList arrayList = new ArrayList();
arrayList.add(new Users(1,"张三1","123",12.34));
arrayList.add(new Users(2,"张三2","12322",1244.34));
arrayList.add(new Users(3,"张三3","12333",1255.34));
JSONArray jsonArray = JSONArray.fromObject(arrayList);//json
System.out.println(jsonArray);
//json->java集合
String str2="[{\"money\":12.34,\"password\":\"123\",\"uid\":1,\"username\"" +
":\"张三1\"},{\"money\":1244.34,\"password\":\"12322\",\"uid\":2,\"username\"" +
":\"张三2\"},{\"money\":1255.34,\"password\":\"12333\",\"uid\":3,\"username\"" +
":\"张三3\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(str2);
Object[] o1 = (Object[]) JSONArray.toArray(jsonArray1, Users.class);
for(Object o2 : o1){
System.out.println("o2="+o2);
}
}
}
index.jsp
<%--
Created by IntelliJ IDEA.
User: 28734
Date: 2021/9/5
Time: 9:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<script>
function test(){
//发送异步请求
//1.创建XMLHttpRequest对象
var xmlhttp;
if(window.XMLHttpRequest){
//非IE
xmlhttp=new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//2.打开链接
var uname=document.getElementById("uname").value;
//xmlhttp.open("get","/testuname?username="+uname,true);
xmlhttp.open("post","/testuname",true);
//3.指定回调函数
xmlhttp.onreadystatechange=function (){
//3.1 判断状态
if(xmlhttp.readyState==4){
//3.2 接收返回的数据
var responseText = xmlhttp.responseText;
document.getElementById("rs").innerText=responseText;
}
}
//4.发送数据
//xmlhttp.send();
//post
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("username="+uname);
}
</script>
<body>
<h1>index.jsp</h1>
username:<input type="text" id="uname" onblur="test()"><span id="rs"></span>
</body>
</html>
jquery_ajax
<%--
Created by IntelliJ IDEA.
User: 28734
Date: 2021/9/5
Time: 9:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function (){
$("#uname").blur(function (){
//1.获得value值
var uname = $(this).val();
//2.发送请求
/*$.ajax({
url:"/testuname",
data:"username="+uname,
type:"post",
dataType:"text",
success:function (rs){
$("#rs").html(rs);
}
})*/
/*$.get("/testuname","username="+uname,function (rs){
$("#rs").html(rs);
});
});*/
$.post("/testuname","username="+uname,function (rs){
$("#rs").html(rs);
});
});
})
</script>
<body>
<h1>jquery_ajax.jsp</h1>
username:<input type="text" id="uname"><span id="rs"></span>
</body>
</html>
testjson.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
//1.定义json
var j1={"name":"张三","age":18};
//alert(j1.name+","+j1.age);
//数组
var j2={"names":["aa","bb","cc"]};
//alert(j2.names[1]);
//json数组内容
var j3={"users":[{"name":"张三1","age":18},{"name":"张三2","age":14},{"name":"张三3","age":16}]}
alert(j3.users[2].name);
</script>
</head>
<body>
</body>
</html>
testjsondemo.jsp
<%--
Created by IntelliJ IDEA.
User: 28734
Date: 2021/9/5
Time: 12:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function (){
$("#uid").blur(function (){
//1.获得value值
var uid = $(this).val();
//2.发送请求
$.post("/getuser","uid="+uid,function (rs){
//转换成可以处理的js
rs=eval("("+rs+")");
//此时的rs是json格式的对象
$("#name").val(rs.username);
$("#pass").val(rs.password);
$("#money").val(rs.money);
});
});
})
</script>
<body>
uid:<input type="text" id="uid"><br>
name:<input type="text" id="name"><br>
pass:<input type="text" id="pass"><br>
money:<input type="text" id="money"><br>
</body>
</html>