jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>使用json格式的数据</title>
</head>
<body>
<script type="text/javascript">
//发起ajax请求,传参给服务器,服务器返回数据
dosearch=function () {
//1.创建异步对象
let XMLHttp=new XMLHttpRequest();
//2.绑定事件
XMLHttp.onreadystatechange=function () {
//处理
if (XMLHttp.readyState==4&&XMLHttp.status==200){
let data=XMLHttp.responseText;
//将字符串转化为json格式
let jsonobj=eval("("+data+")");
callback(jsonobj);
}
}
let provinceid=document.getElementById("provinceid").value;
let param="provinceid="+provinceid;
//3
XMLHttp.open("get","sea?"+param,true);
//4
XMLHttp.send();
}
//调用函数实现json数据赋值给文本框
function callback (json) {
document.getElementById("name").value=json.name;
document.getElementById("sname").value=json.jiancheng;
document.getElementById("city").value=json.shenghui;
}
</script>
<table border="2px" align="center">
<tr>
<td>省份编号</td>
<td><input type="text" id="provinceid"></td>
<td><input type="button" value="搜索" onclick="dosearch()"></td>
</tr>
<tr>
<td>省份名称</td>
<td colspan="2"><input type="text" id="name"></td>
</tr>
<tr>
<td>省份简称</td>
<td colspan="2"><input type="text" id="sname"></td>
</tr>
<tr>
<td>省会城市</td>
<td colspan="2"><input type="text" id="city"></td>
</tr>
</table>
</body>
</html>
Servlet代码
package syf;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import wff.Province;
import java.io.IOException;
import java.io.PrintWriter;
public class searchServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("provinceid");
String p="";
response.setContentType("text/html;charset=utf-8");
if (id!=""&&null!=id){
proviceDao name=new proviceDao();
Integer procinceid= Integer.parseInt(id);
Province x=name.Provinceget(procinceid);
//需要使用jackson 把province对象转换为json对象
ObjectMapper json=new ObjectMapper();
p= json.writeValueAsString(x);
}
PrintWriter pw=response.getWriter();
pw.print(p);
pw.flush();
pw.close();
}
}
Dao
public Province Provinceget(Integer id){
Province sname=null;
ResultSet res=null;
String sql="select id,name,jiancheng,shenghui from province where id=?";
PreparedStatement pre=jdbc.createStatement(sql);
try {
pre.setInt(1,id);
res=pre.executeQuery();
while (res.next()){
sname=new Province();
sname.setId(res.getInt("id"));
sname.setName(res.getString("name"));
sname.setJiancheng(res.getString("jiancheng"));
sname.setShenghui(res.getString("shenghui"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return sname;
}
Province
package wff;
public class Province {
private Integer id;
private String name;
private String jiancheng;
private String shenghui;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJiancheng() {
return jiancheng;
}
public void setJiancheng(String jiancheng) {
this.jiancheng = jiancheng;
}
public String getShenghui() {
return shenghui;
}
public void setShenghui(String shenghui) {
this.shenghui = shenghui;
}
}
还有一个自己封装的jdbc工具