日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

web前后台数据交互

發(fā)布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的用戶數據??蛻舻谝淮卧L問支持session的JSP網頁,服務器會創(chuàng)建一個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); //網址不會改變(這是轉發(fā))

PS:如果后臺使用的轉發(fā)代碼為 response.sendRedirect(“welcome.jsp”); //網址變?yōu)閣elcome.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來存儲,例如用戶名,登錄狀態(tài),購物車信息顯示在網頁上的信息數據大多通過 request或Ajax方式獲取

  • ssm框架下,js頁面通過json將數據發(fā)送到后臺,后臺處理之后,再將數據發(fā)送到前臺。

  • 在前臺,要將用戶名和郵箱發(fā)送到后臺,先將用戶名和和郵箱轉成json形式的數據,在通過ajax發(fā)送到后臺,其中url為后臺要處理數據的地址。前臺主要代碼如下,其中User是一個實體類,有id,name,email,password等屬性。

  • var user_json = { "user_name": user_name, "user_mail":user_mail } //js對象轉換成JSON字符串 var jason_str = JSON.stringify(user_json); //Ajax發(fā)送數據給后臺 $.ajax({ url :"/MyApplication/user/checkUserLogin", cache : true, type : "post", datatype : "json", contentType : "application/json; charset=utf-8", data : jason_str, success : function (data){ //返回空值 if(data==null){ alert("不存在該用戶!"); window.location.href = "sighIn.html"; } else{ //存在該用戶 if(password == data.user_password){ //密碼正確 window.location.href = "index.html?user_name=" + data.user_name; }else{ //密碼不正確 alert("密碼錯誤!"); window.location.href = "sighIn.html"; } } } });

    后臺只要代碼如下(在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發(fā)送到前端。

    去年開始接觸java web項目開發(fā),在項目開發(fā)過程中難免會遇到前臺jsp頁面獲得的數據傳到后臺controller層去處理,對于常用的三種方式進行了以下總結:

  • Form表單提交
  • jsp頁面中可以嵌入form表單,主要有兩個屬性,action和method。action的內容是表單要提交到后臺controller的某個請求。method是表單提交方式:主要有get和post兩種提交方式,一般的表單提交數據會用到post方式,考慮到數據安全性問題。下面是我做的一個小例子,有用戶名和密碼兩個字段

    jsp頁面form表單

    后臺處理請求代碼:

    后臺請求方法

  • Ajax = Asynchronous JavaScript and XML
  • 通過在后臺與服務器進行少量數據交換,AJAX 可以使網頁實現異步更新。這意味著可以在不重新加載整個網頁的情況下,對網頁的某部分進行刷新。很常見的例子在某些網站注冊過程中要求用戶名不能重復,所以在避免數據已經提交到后臺去數據庫校驗該用戶是否存的的情況下,ajax可以實現異步刷新,在文本框失去焦點后就去訪問后臺數據庫判斷該用戶是否已經存在。

    jquery中的ajax

    下面簡單介紹下ajax請求里面的主要幾個參數:

  • url:一般為String類型的參數,發(fā)送請求的地址。
  • type:一般String類型的參數,請求方式主要用(post或get)默認為get。
  • data:一般為Object或String類型的參數,發(fā)送到服務器的數據。如果已經不是字符串,將自動轉換為字符串格式
  • dataType:預期服務器返回的數據類型
  • success:要求為Function類型的參數,請求成功后調用的回調函數,一般有兩個參數。
    (1)由服務器返回,并根據dataType參數進行處理后的數據。
    (2)描述狀態(tài)的字符串。
    function(data, textStatus){
    //data可能是xmlDoc、jsonObj、html、text等等
    }
  • error:請求失敗時被調用的函數
  • a標簽中src屬性
  • 常用的a便簽中的src屬性也可以發(fā)送請求到后臺,后臺有相應的處理方法即可。
    a便簽

    后臺處理方法
    以上三種方式是我總結的工作中比較常用的幾種方法,尤其是form表單提交數據。初次發(fā)稿還有許多不足的地方,歡迎大家繼續(xù)補充和提出意見哈。

    總結

    以上是生活随笔為你收集整理的web前后台数据交互的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。