student.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.7.1.min.js"></script>
<script src="js/student.js"></script>
</head>
<body>
<table border='1'>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>sex</th>
</tr>
</thead>
</tbody>
<tr>
<td>1</td>
<td>张三</td>
<td>20</td>
<td>男</td>
</tr>
</tbody>
</table>
</body>
</html>
student.js
//查找
$.ajax({
url:"SearchServlett",
type:"get",
success:function(value){
var arr = value.data
for(var i=0;i<arr.length;i++){
$("tbody").append("<tr>"+
"<td>"+arr[i].id+"</td>"+
"<td>"+arr[i].name+"</td>"+
"<td>"+arr[i].sex+"</td>"+
"</tr>")
}
},
error:function(){
alert("出错啦!")
},
})
SearchSelvert
package com.qc.servlet;
import java.io.IOException;
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 com.qc.db.MysqlUtil;
/**
* Servlet implementation class SearchServlett
*/
@WebServlet("/SearchServlett")
public class SearchServlett extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SearchServlett() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sql="select * from student";
String[] colums= {"id","sex","age","name"};
String res=MysqlUtil.getJsonBySql(sql, colums);
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(res);
}
/**
* @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);
}
}