web前后台数据交互
1.利用cookie對象
Cookie是服務器保存在客戶端中的一小段數據信息。使用Cookie有一個前提,就是客戶端瀏覽器允許使用Cookie并對此做出相應的設置。一般不贊成使用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的用戶數據。客戶第一次訪問支持session的JSP網頁,服務器會創建一個session對象記錄客戶的信息。當客戶訪問同一網站的不同網頁時,仍處于同一個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重定向,設置setAttribute
(1)后臺代碼
request.setAttribute( "name" , "cute" ); request.getRequestDispatcher( "welcome.jsp" ).forward(request, response); //網址不會改變(這是轉發)PS:如果后臺使用的轉發代碼為 response.sendRedirect(“welcome.jsp”); //網址變為welcome.jsp(這是重定向)
則request設置的參數無效,因為已經切換到另一個請求了,request參數的有效期為本次請求。
(2)前臺代碼
String name=request.getAttribute( "name" ).toString();4.利用Ajax進行異步數據請求(得到的數據可以以json或xml格式返回,便于處理)
(1)后臺代碼案例(運用servlet傳輸數據)
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請求處理數據代碼
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; } //獲取數據的函數 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(); }總結:在用戶訪問網站整個生命周期中都會用到的數據用session來存儲,例如用戶名,登錄狀態,購物車信息顯示在網頁上的信息數據大多通過 request或Ajax方式獲取
ssm框架下,js頁面通過json將數據發送到后臺,后臺處理之后,再將數據發送到前臺。
在前臺,要將用戶名和郵箱發送到后臺,先將用戶名和和郵箱轉成json形式的數據,在通過ajax發送到后臺,其中url為后臺要處理數據的地址。前臺主要代碼如下,其中User是一個實體類,有id,name,email,password等屬性。
后臺只要代碼如下(在controller層編寫)
@RequestMapping(value = "/checkUserLogin") public @ResponseBody User checkUserLogin(@RequestBody User user){ if (user.getUser_name() != null) { user = userService.findByName(user.getUser_name()); } else if (user.getUser_mail() != null) { user = userService.findByMail(user.getUser_mail()); } return user; }@RequestBody是將json形式的數據轉化成User類型的數據,@ResponseBody是將User類型的數據轉成json發送到前端。
去年開始接觸java web項目開發,在項目開發過程中難免會遇到前臺jsp頁面獲得的數據傳到后臺controller層去處理,對于常用的三種方式進行了以下總結:
jsp頁面中可以嵌入form表單,主要有兩個屬性,action和method。action的內容是表單要提交到后臺controller的某個請求。method是表單提交方式:主要有get和post兩種提交方式,一般的表單提交數據會用到post方式,考慮到數據安全性問題。下面是我做的一個小例子,有用戶名和密碼兩個字段
jsp頁面form表單
后臺處理請求代碼:
后臺請求方法
通過在后臺與服務器進行少量數據交換,AJAX 可以使網頁實現異步更新。這意味著可以在不重新加載整個網頁的情況下,對網頁的某部分進行刷新。很常見的例子在某些網站注冊過程中要求用戶名不能重復,所以在避免數據已經提交到后臺去數據庫校驗該用戶是否存的的情況下,ajax可以實現異步刷新,在文本框失去焦點后就去訪問后臺數據庫判斷該用戶是否已經存在。
jquery中的ajax
下面簡單介紹下ajax請求里面的主要幾個參數:
(1)由服務器返回,并根據dataType參數進行處理后的數據。
(2)描述狀態的字符串。
function(data, textStatus){
//data可能是xmlDoc、jsonObj、html、text等等
}
常用的a便簽中的src屬性也可以發送請求到后臺,后臺有相應的處理方法即可。
a便簽
后臺處理方法
以上三種方式是我總結的工作中比較常用的幾種方法,尤其是form表單提交數據。初次發稿還有許多不足的地方,歡迎大家繼續補充和提出意見哈。
總結
以上是生活随笔為你收集整理的web前后台数据交互的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯和阿里巴巴考虑互相开放生态,是真开放
- 下一篇: 互联网晚报 | 9月2日 星期四 | 小