java对象
这是uservo定义的源码
package VO.userVO;
import PO.userPO.UserPO;
public class UserVO {
private String account;//用户账户,也是用户唯一标志,登录时使用,每个用户的账户是唯一的。账户格式要求只能包含英文字母、数字,且必须以英文字母开头。
private String username;//用户名,即用户昵称,类似与qq昵称
private String password;//用户密码
private String realName;//用户真实姓名
private String mail;//邮箱
private String birth;//生日,8位,如“19970516”
private String phone;//手机号码,11位
public UserVO() {
}
public UserVO(String account, String username, String password) {
this.account=account;
this.username = username;
this.password = password;
}
public UserVO(String account, String username, String password, String realName, String mail, String birth, String phone) {
this.account=account;
this.username = username;
this.password = password;
this.realName = realName;
this.mail = mail;
this.birth = birth;
this.phone = phone;
}
public UserVO(UserPO userPO){
this.account=userPO.getAccount();
this.username = userPO.getUsername();
this.password = userPO.getPassword();
this.realName = userPO.getRealName();
this.mail = userPO.getMail();
this.birth = userPO.getBirth();
this.phone = userPO.getPhone();
}
//get and set
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
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 String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
servlet
以下是servlet,将UserVO的ArrayList转换为json,之后response。
package Servlet.UserBlSer;
import VO.userVO.UserVO;
import bl.userBl.User;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class register extends HttpServlet {
private static final long serialVersionUID = 1L;
private User user;
/**
* @see HttpServlet#HttpServlet()
*/
public register(){
super();
user=new User();
}
public static void main(String[] args){
System.out.println(123);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<UserVO> userVOS=new ArrayList<>();
UserVO userVO1=new UserVO("111","wxf","123");
UserVO userVO2=new UserVO("222","czf","456");
UserVO userVO3=new UserVO("333","wcy","789");
userVOS.add(userVO1);
userVOS.add(userVO2);
userVOS.add(userVO3);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JSONArray jsonarray = new JSONArray();
try {
for (int i = 0; i < userVOS.size(); i++) {
JSONObject jsonObject=new JSONObject();
jsonObject.put("account",userVOS.get(i).getAccount());
jsonObject.put("username",userVOS.get(i).getUsername());
jsonObject.put("password",userVOS.get(i).getPassword());
jsonarray.put(jsonObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
PrintWriter pw = response.getWriter();
pw.write(jsonarray.toString());
pw.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
ajax
最后是ajax获取json数组,并显示。
function getDataPack() {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var result=xmlhttp.responseText;
var dataPack=eval(result);
var output="";
for(var i=0;i<dataPack.length;i++){
output=output+dataPack[i].account+" "+dataPack[i].username+" "+dataPack[i].password+"\n";
}
document.getElementById("test").innerHTML=output;
}
}
xmlhttp.open("GET","/StockHubWeb/register",true);
xmlhttp.send();
}
http代码
以下是http代码,网页显示,按按钮后调用js方法,修改id为“test”的组件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test1</title>
<script src="js/test.js"></script>
</head>
<body>
<h1 id="test">初始</h1>
<button type="button" onclick="getDataPack()">搜索</button>
</body>
</html>
以上就是全部内容,希望对大家有所帮助!