功能:点击按钮,后端数据库信息输出到前端浏览器页面上。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取数据库信息title>
head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("btn").onclick = function (){
//1.创建核心对象
var xhr = new XMLHttpRequest();
//2.注册回调函数
xhr.onreadystatechange = function(){
if (this.readyState == 4){
if(this.status == 200){
document.getElementById("tr1").innerHTML = this.responseText;
}else{
alert(this.status);
}
}
}
//3.开启通道
xhr.open("get","/xmm/servlet3",true);
//4.发送请求
xhr.send();
}
}
script>
<input type="button" id="btn" value="点击"/><br/>
<table>
<tr>
<th>deptnoth>
<th>dnameth>
<th>locth>
tr>
<tbody id="tr1">
tbody>
table>
body>
html>
package mypackage;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ResourceBundle;
@WebServlet("/servlet3")
public class Servlet03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
StringBuilder sb = new StringBuilder();
Connection con = null;
PreparedStatement pre = null;
ResultSet res = null;
try {
Class.forName(rb.getString("driver"));
con = DriverManager.getConnection(rb.getString("url"),rb.getString("username"),rb.getString("password"));
pre = con.prepareStatement("select * from dept");
res = pre.executeQuery();
while(res.next()){
int deptno = res.getInt("deptno");
String dname = res.getString("dname");
String loc = res.getString("loc");
sb.append("");
sb.append(""+deptno+" ");
sb.append(""+dname+" ");
sb.append(""+loc+" ");
sb.append(" ");
}
out.print(sb);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
pre.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取数据库信息title>
head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("btn").onclick = function (){
//1.创建核心对象
var xhr = new XMLHttpRequest();
//2.注册回调函数
xhr.onreadystatechange = function(){
if (this.readyState == 4){
if(this.status == 200){
var t = JSON.parse(xhr.responseText);
var html = "";
for (var i = 0; i < t.length; i++) {
var s = t[i];
html+=("");
html+=(""+s.deptno+" ");
html+=(""+s.dname+" ");
html+=(""+s.loc+" ");
html+=(" ");
}
document.getElementById("tr1").innerHTML=html;
}else{
alert(this.status);
}
}
}
//3.开启通道
xhr.open("get","/xmm/servlet4",true);
//4.发送请求
xhr.send();
}
}
script>
<input type="button" id="btn" value="点击"/><br/>
<table>
<tr>
<th>deptnoth>
<th>dnameth>
<th>locth>
tr>
<tbody id="tr1">
tbody>
table>
body>
html>
package mypackage;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ResourceBundle;
@WebServlet("/servlet4")
public class Servlet04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
StringBuilder sb = new StringBuilder();
Connection con = null;
PreparedStatement pre = null;
ResultSet res = null;
try {
Class.forName(rb.getString("driver"));
con = DriverManager.getConnection(rb.getString("url"),rb.getString("username"),rb.getString("password"));
pre = con.prepareStatement("select * from dept");
res = pre.executeQuery();
sb.append("[");
while(res.next()){
int deptno = res.getInt("deptno");
String dname = res.getString("dname");
String loc = res.getString("loc");
String jsonString = "{\"deptno\":"+deptno+", \"dname\":\""+dname+"\", \"loc\":\""+loc+"\" }";
sb.append(jsonString+",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("]");
/*
拼成的实际上是一个json对象的数组:
[
{
"deptno":01
"dname":"指挥部"
"loc":"北京"
},
{
"deptno":02
"dname":"执行部"
"loc":"上海"
}
]
*/
out.print(sb);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
pre.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}