注意:要将json的相关类和json.js事先放到相关目录下面
1.AddUser.java
package com.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
public class AddUser extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuffer json = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
while((line = reader.readLine())!=null){
line = new String(line.getBytes("ISO8859-1"),"utf-8");
json.append(line);
}
JSONObject jsonObject = null;
try{
jsonObject = new JSONObject(json.toString());
}catch(Exception e){
e.printStackTrace();
}
String username = null;
String password = null;
String department = null;
String headship = null;
String sex = null;
String old = null;
try{
username = jsonObject.getString("username").toUpperCase();
password = jsonObject.getString("password");
department = jsonObject.getString("department");
headship = jsonObject.getString("headship");
sex = jsonObject.getString("sex");
old = jsonObject.getString("old")+"岁";
if(sex.equals("1")){
username = username+"先生";
sex = "男";
}else{
username = username+"女士";
sex = "女";
}
jsonObject.put("username", username);
jsonObject.put("password", password);
jsonObject.put("department", department);
jsonObject.put("headship", headship);
jsonObject.put("sex", sex);
jsonObject.put("old", old);
}catch(Exception e){
e.printStackTrace();
}
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println(jsonObject.toString());
out.flush();
out.close();
}
}
2.index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="json.js">
</script>
<script type="text/javascript">
var xmlhttp;
function createXmlHttpResquest() {
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
}
function addNew() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var department = document.getElementById("department").value;
var headship = document.getElementById("headship").value;
var sex = document.getElementById("sex").value;
var old = document.getElementById("old").value;
if (username == "") {
window.alert("请输入姓名!");
return;
}
if (password == "") {
window.alert("请输入密码!");
return;
}
if (department == "") {
window.alert("请输入部门!");
return;
}
if (headship == "") {
window.alert("请输入职务!");
return;
}
if (old == "") {
window.alert("请输入年龄!");
return;
}
var newUser = new User(username, password, department, headship, sex, old);
var userAsJSON = JSON.stringify(newUser);
createXmlHttpResquest();
xmlhttp.open("GET", "addUser", true);
xmlhttp.onreadystatechange = processor;
xmlhttp.send(userAsJSON);
}
function processor() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var user = JSON.parse(xmlhttp.responseText);
var username = user.username;
var password = user.password;
var department = user.department;
var headship = user.headship;
var sex = user.sex;
var old = user.old;
updateTable(username, password, department, headship, sex, old);
}
}
}
function User(username, password, department, headship, sex, old) {
this.username = username;
this.password = password;
this.department = department;
this.headship = headship;
this.sex = sex;
this.old = old;
}
function updateTable(username, password, department, headship, sex, old) {
document.all.userTable.insertRow(-1);
var rows = document.all.userTable.rows.length;
document.all.userTable.rows[rows - 1].bgcolor = "#ffffff";
document.all.userTable.rows[rows - 1].align = "center";
//在插入的一行中插入六列
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].insertCell(-1);
document.all.userTable.rows[rows - 1].cells[0].innerText = username;
document.all.userTable.rows[rows - 1].cells[1].innerText = password;
document.all.userTable.rows[rows - 1].cells[2].innerText = department;
document.all.userTable.rows[rows - 1].cells[3].innerText = headship;
document.all.userTable.rows[rows - 1].cells[4].innerText = sex;
document.all.userTable.rows[rows - 1].cells[5].innerText = old;
}
</script>
</head>
<body>
<h2>
AJAX+JSON示例
</h2>
<hr>
姓名:
<input name="userName" id="username" type="text">
<br>
密码:
<input name="password" id="password" type="text">
<br>
部门:
<input name="department" id="department" type="text">
<br>
职务:
<input name="headship" id="headship" type="text">
<br>
性别:
<SELECT id="sex">
<option value="1">
男
</option>
<option value="2">
女
</option>
</SELECT>
<br>
年龄:
<input name="old" id="old" type="text">
<br>
<input type="button" name="btn" value="新增用户" onClick="addNew()">
<br>
<br>
<TABLE id="userTable" border="1" width="500px" bgcolor="#eeeeee">
<TR>
<TD align="center">
姓名
</TD>
<TD align="center">
密码
</TD>
<TD align="center">
部门
</TD>
<TD align="center">
职务
</TD>
<TD align="center">
性别
</TD>
<TD align="center">
年龄
</TD>
</TR>
</TABLE>
</body>
</html>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>addUser</servlet-name>
<servlet-class>com.servlet.AddUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addUser</servlet-name>
<url-pattern>/addUser</url-pattern>
</servlet-mapping>
</web-app>
分享到:
相关推荐
json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全json使用大全
json使用教程
json 使用资料json 使用资料json 使用资json 使用资料料
java使用superMap解析DWG文件为json输出,本地通过,附件中为word操方式按照步骤可以导入数据集
android json使用方法,使用json完成java与c++数据封装传输
Json格式使用需要使用的依赖包,这里给提供,使用时导入,避免大家的查找麻烦
json使用联系,包括序列化的使用以及在socket中的一点应用,使用linux下c++编写
json lib net.sf.json使用的所有jar包,不用找jar包了。json lib是很好的json解析工具类。
C# JSON库 Newtonsoft.Json 使用范例源码(双向动态生成,无需事先定义类): 1. 将字典类型序列化成Json字符串 2. 将Json字符串,反序列化成字典类型 3. 动态添加字典类型并序列化为Json字符串 4. 根据Json字符...
json使用样类,c++代码demo,使用c++进行json协议通信,测试可用
Json 使用简单实例 c++ 版,内含 VS2008 工程文件
linux下的json格式数据打包与解析,可用于网络交互,比较容易上手。
NULL 博文链接:https://liujianeye.iteye.com/blog/1150049
主要是json使用时要用到的jar包,里面我做了一些和Struts2整合时遇到jar包冲突问题时的解决办法简介
把网上所有关于Newtonsoft.Json使用方法进行了总结,里面包括Json的序列化和发序列化,还有日期的处理。希望对大家有用。
java配合json使用的JAR,版本配套9个JAR
net.sf.json使用的lib(包括依赖lib) 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar
json使用-bookmanager
第六章 Caché JSON 使用JSON适配器
json object string 使用开发说明