java實(shí)現(xiàn)后臺(tái)數(shù)據(jù)顯示在前端
本篇使用servlet +.ajax( )的技術(shù),實(shí)現(xiàn)簡(jiǎn)單的前后臺(tái)的交互問(wèn)題。
首先來(lái)了解一下AJAX
AJAX是jquery的一個(gè)方法,一種在網(wǎng)頁(yè)上調(diào)用后臺(tái)接口的方式。
示例:$.ajax( { 參數(shù) } ) ;
$.ajax()等同于jQuery.ajax( )
參數(shù)里是一個(gè)JS對(duì)象, 其中的屬性:
type: ' GET' /‘POST'
url: 接口地址
success:服務(wù)器應(yīng)答時(shí),調(diào)用此function處理(回調(diào)方法)
另外說(shuō)一下Servlet
Servlet,服務(wù)小程序,為客戶端提供自定義服務(wù)的機(jī)制。
瀏覽器(客戶端) —請(qǐng)求—》Tomcat(服務(wù)器)
Tomcat(服務(wù)器) —應(yīng)答—》瀏覽器(客戶端)
//創(chuàng)建一個(gè)學(xué)生pojo 類
/**
* 這是一個(gè)關(guān)于學(xué)生的POJO類
* 暫時(shí)不引入數(shù)據(jù)庫(kù)
* 只是一個(gè)假的數(shù)據(jù)庫(kù)
*/
public class Student
{
public int id;
public String name;
public String adress;
public Student()
{
}
public Student(int id,String name,String adress)
{
this.id = id;
this.name = name;
this.adress = adress;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAdress()
{
return adress;
}
public void setAdress(String adress)
{
this.adress = adress;
}
創(chuàng)建一個(gè)假的學(xué)生類型數(shù)據(jù)庫(kù)并存入數(shù)據(jù)
public class Db
{
//創(chuàng)建一個(gè)本類的全局對(duì)象
public static Db i = new Db();
//使用鏈表寫入數(shù)據(jù)
private ArrayList<Student> stu = new ArrayList<>();
private Db()
{
stu.add(new Student(20180001,"老王","北京"));
stu.add(new Student(20180002,"老甄","邢臺(tái)"));
stu.add(new Student(20180003,"老高","邢臺(tái)"));
stu.add(new Student(20180004,"老孟","邯鄲"));
stu.add(new Student(20180005,"老裴","太原"));
stu.add(new Student(20180006,"老李","東北"));
stu.add(new Student(20180007,"老張","武漢"));
stu.add(new Student(20180008,"老苗","重慶"));
stu.add(new Student(20180009,"老郭","北京"));
}
//獲取全部信息
public ArrayList<Student> all()
{
return stu;
}
//按照學(xué)號(hào)查詢
public ArrayList<Student> byId(int from,int to)
{
ArrayList<Student> qStu = new ArrayList<>();
for(int i=0;i<stu.size();i++)
{
Student s = stu.get(i);
if(s.id<=from &&s.id<=to)
{
qStu.add(s);
}
}
return qStu;
}
}
創(chuàng)建一個(gè)servlet
將數(shù)據(jù)返回
/**
*只需要更改doGet方法
*/
@WebServlet("/QueryAll")
public class QueryAll extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Student> rows = Db.i.all();
//轉(zhuǎn)換成JSON格式
JSONArray result = new JSONArray(rows);
//返回?cái)?shù)據(jù)
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
Writer writer = response.getWriter();
writer.write(result.toString(2));
writer.close();
}
}
下面是前端的代碼 將html+css+js結(jié)合到了一起
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
<style>
body{
background-color: #EEEEEE;
margin: 0px;
padding: 0px;
}
table{
border-collapse: collapse;
table-layout: fixed;
}
table,td,th{
border: 1px solid #888;
text-align: center;
}
.main{
width: 600px;
height: 300px;
background-color: #FFFFFF;
padding: 10px;
margin: 10px auto;
position: relative;
}
.main .content{
width: 300px;
}
.empty{
text-align: center;
padding: 4px;
display: block;
border: 0px solid #888;
border-width: 0px 1px 1px 1px;
}
.main .id{ width: 100px;}
.main .name{width: 100px;}
.main .adress{width: 100px;}
</style>
</head>
<body>
<div class="main">
<button onclick="query()">查詢</button>
<div class="content">
<table>
<thead>
<tr>
<th class="id">學(xué)號(hào)</th>
<th class="name">姓名</th>
<th class="adress">住址</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="empty">
現(xiàn)在沒(méi)有數(shù)據(jù)
</div>
</div>
</div>
</body>
<script>
//查詢
function query()
{
$.ajax({
type:"GET";
url:"QueryAll";
dataType:"json";
success:function(resp)
{
show(resp);
}
});
}
//格式化數(shù)據(jù)并顯示
function show(result)
{
var cont = $(".main tbody");
cont.html(""); //清空
for(var row of result)
{
var str = "<tr>"
+"<td>"+row.id+"</td>"
+"<td>"+row.name+"</td>"
+"<td>"+row.adress+"</td>"
+"</tr>";
cont.append(str);
}
//沒(méi)有數(shù)據(jù)把空的內(nèi)容顯示出來(lái)
if(result.length>0)
$(".empty").hide();
else
$(".empty").show();
}
</script>
</html>
最后實(shí)現(xiàn)的內(nèi)容

當(dāng)點(diǎn)擊這個(gè)查詢的時(shí)候 ,將學(xué)生信息打印出來(lái)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了java語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Java實(shí)現(xiàn)拓?fù)渑判虻氖纠a
這篇文章我們要講的是拓?fù)渑判?,這是一個(gè)針對(duì)有向無(wú)環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05
java實(shí)現(xiàn)讀取jar包中配置文件的幾種方式
本文主要介紹了java實(shí)現(xiàn)讀取jar包中配置文件的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
java簡(jiǎn)單讀取properties配置文件的方法示例
這篇文章主要介紹了java簡(jiǎn)單讀取properties配置文件的方法,涉及java針對(duì)properties配置的載入及文件屬性讀取相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
JAVA?Springboot配置i18n國(guó)際化語(yǔ)言詳細(xì)步驟
國(guó)際化(Internationalization,縮寫為i18n)是指根據(jù)來(lái)展示不同的內(nèi)容,使應(yīng)用程序能夠適應(yīng)不同的語(yǔ)言和文化習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于JAVA?Springboot配置i18n國(guó)際化語(yǔ)言的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問(wèn)題的幾種方法
本篇文章主要介紹了解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問(wèn)題的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Eclipse中如何引入JUnit進(jìn)行單元測(cè)試
這篇文章主要介紹了Eclipse中如何引入JUnit進(jìn)行單元測(cè)試問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Java?Spring?boot實(shí)現(xiàn)生成二維碼
大家好,本篇文章主要講的是Java?Spring?boot實(shí)現(xiàn)生成二維碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
解決OkHttp接收gzip壓縮數(shù)據(jù)返回亂碼問(wèn)題
這篇文章主要介紹了解決OkHttp接收gzip壓縮數(shù)據(jù)返回亂碼問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

