前段后臺交互的學(xué)習(xí)(Java web)
標簽 : 前后端交互 Javaweb
下面介紹了一些關(guān)于交互的細節(jié):
- 請求數(shù)據(jù)
- 前端提供請求數(shù)據(jù)馍忽。
在開發(fā)中,后臺在查詢數(shù)據(jù)庫時棒坏,需要借助查詢條件才能查詢到前端需要的數(shù)據(jù),而查詢條件正是此時前端提供相關(guān)的查詢參數(shù)(即URL請求的參數(shù))
- 接口文檔
- 接口文檔主要由后臺設(shè)計和修改遭笋。
后臺直接跟數(shù)據(jù)打交道坝冕,最熟悉數(shù)據(jù)庫。前端只是數(shù)據(jù)的接受者和接口文檔的使用者瓦呼。
- 交互數(shù)據(jù)的格式
- 主要是JSON喂窟,以及不常用的XML。
JSON通常用于與服務(wù)器交換數(shù)據(jù)央串,AJAX也是通過JSON數(shù)據(jù)來完成交互磨澡。
- 交互原理
- 前端根據(jù)接口,提供請求參數(shù)质和,后臺接收參數(shù)稳摄,根據(jù)參數(shù)查詢數(shù)據(jù)庫,并得到返回的數(shù)據(jù)饲宿,將返回的參數(shù)送到前端厦酬。前端調(diào)用接口,將返回的數(shù)據(jù)呈現(xiàn)瘫想。
- 請求方法
- 請求方法主要有POST和GET仗阅,GET是向服務(wù)器發(fā)索取數(shù)據(jù)的一種請求,而POST是向服務(wù)器提交數(shù)據(jù)(提交表單)的一種請求国夜。
下面以Java web講述前后端的交互方式:
1. 利用cookie對象
Cookie是服務(wù)器保存在客戶端中的一小段數(shù)據(jù)信息减噪。使用Cookie有一個前提,就是客戶端瀏覽器允許使用Cookie并對此做出相應(yīng)的設(shè)置支竹。一般不贊成使用Cookie旋廷。
(1)后端代碼
Cookie cookie=new Cookie("name", "hello");
response.addCookie(cookie);
(2)前端代碼
Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().toString().equals("name")){
out.print(cookies[i].getValue());
}
}
2. 利用session對象
session對象表示特定會話session的用戶數(shù)據(jù)±窀椋客戶第一次訪問支持session的JSP網(wǎng)頁饶碘,服務(wù)器會創(chuàng)建一個session對象記錄客戶的信息。當(dāng)客戶訪問同一網(wǎng)站的不同網(wǎng)頁時馒吴,仍處于同一個session中扎运。
(1)后端代碼
request.getSession().setAttribute("name", name);
request.getSession().setMaxInactiveInterval(2);
response.sendRedirect("welcome.jsp");
(2)前端代碼(jsp頁面)
Object user=request.getSession().getAttribute("name");
3. 利用request重定向瑟曲,設(shè)置setAttribute
(1)后端代碼
request.setAttribute("name", "cute");
request.getRequestDispatcher("welcome.jsp").forward(request, response); //網(wǎng)址不會改變
PS:如果后臺使用的轉(zhuǎn)發(fā)代碼為 response.sendRedirect("welcome.jsp"); //網(wǎng)址變?yōu)閣elcome.jsp
則request設(shè)置的參數(shù)無效,因為已經(jīng)切換到另一個請求了豪治,request參數(shù)的有效期為本次請求洞拨。
(2)前端代碼
String name=request.getAttribute("name").toString();
4. 利用Ajax進行異步數(shù)據(jù)請求(得到的數(shù)據(jù)可以以json或xml格式返回,便于處理)
(1)后端代碼案例(運用servlet傳輸數(shù)據(jù))
public class TestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String data="[{\"name\":\"apple\",\"price\":23},{\"name\":\"banana\",\"price\":12},{\"name\":\"orange\",\"price\":8}]";
out.write(data);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
(2)前端js請求處理數(shù)據(jù)代碼
function createXMLHttpRequest(){
var xmlrequest;
if(window.XMLHttpRequest){
xmlrequest=new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
xmlrequest=new ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
try{
xmlrequest=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlrequest="";
}
}
}
return xmlrequest;
}
//獲取數(shù)據(jù)的函數(shù)
function change(){
var xmlrequest=createXMLHttpRequest();
xmlrequest.open("POST","TestServlet",true);
xmlrequest.onreadystatechange=function(){
if(xmlrequest.readyState==4&&xmlrequest.status==200){
var data=JSON.parse(xmlrequest.responseText);
var content="<table border=1>";
for(var i=0;i<data.length;i++){
content+="<tr>";
for(o in data[i]){
content+="<td>"+data[i][o]+"</td>";
}
content+="</tr>";
}
content+="</table>";
document.getElementById("test").innerHTML=content;
}
};
xmlrequest.send();
}
總結(jié):在用戶訪問網(wǎng)站整個生命周期中都會用到的數(shù)據(jù)用session來存儲负拟,例如用戶名烦衣,登錄狀態(tài),購物車信息顯示在網(wǎng)頁上的信息數(shù)據(jù)大多通過 request或Ajax方式獲取.
參考文章:
csdn博客