前端代码
$(".mod_post").click(function (e) {
var id = $(".id").val(),
name = $(".name").val(),
english = $(".english").val(),
chinese = $(".chinese").val(),
math = $(".math").val(),
total = $(".total").val();
var map_data_o = {};
var old_data = new Map();
var new_data = new Map();
if (id != idc) {
new_data.set("id", id);
} else {
old_data.set("id", idc);
map_data_o["id"]=idc;
}
if (name != namec) {
new_data.set("name", name);
} else {
old_data.set("name", namec);
}
if (chinese != chinesec) {
new_data.set("chinese", chinese);
} else {
old_data.set("chinese", chinesec);
}
if (math != mathc) {
new_data.set("math", math);
} else {
old_data.set("math", mathc);
}
if (english != englishc) {
new_data.set("english", english);
} else {
old_data.set("english", englishc);
}
if (total != totalc) {
new_data.set("total", total);
} else {
old_data.set("total", totalc);
}
console.log(new_data);
new_data.set("null","null");
console.log(old_data);
console.log(JSON.parse(JSON.stringify(new_data)));
console.log(JSON.parse(JSON.stringify(old_data)));
$.ajax({
type: "post",
url: "/servlet_mod",
data:{
n:Array.from(new_data),
o:Array.from(old_data)
},
dataType: "json",
success: function (response) {
}
});
})
后端代码
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.util.*;
@WebServlet("/servlet_mod")
public class Mod_server_ajax extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, String[]> map = req.getParameterMap();
Map<String,String> n_val=new HashMap<String,String>();
Map<String,String> o_val=new HashMap<String,String>();
boolean n_or_o= true;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry element = (Map.Entry) iter.next();
String[] value = (String[]) element.getValue();
if (value[0].equals("null") && value[1].equals("null")) {
n_or_o = false;
continue;
}
if (n_or_o) {
n_val.put(value[0],value[1]);
} else {
o_val.put(value[0],value[1] );
}
}
Iterator<String> it=n_val.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value= n_val.get(key);
System.out.printf("%s : %s\n",key,value);
}
it=o_val.keySet().iterator();
while (it.hasNext()){
String key = it.next();
String value= o_val.get(key);
System.out.printf("%s : %s\n",key,value);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}