WEB后台傳數據給前台


 

1.利用cookie對象 

Cookie是服務器保存在客戶端中的一小段數據信息。使用Cookie有一個前提,就是客戶端瀏覽器允許使用Cookie並對此做出相應的設置。一般不贊成使用Cookie。

(1)后台代碼

        Cookiecookie=new Cookie("name", "hello");

        response.addCookie(cookie);

(2)前台代碼

   Cookie[]cookies=request.getCookies();

   for(inti=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頁面)

Objectuser=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)前台代碼

Stringname=request.getAttribute("name").toString();

 

4.利用Ajax進行異步數據請求(得到的數據可以以json或xml格式返回,便於處理)

(1)后台代碼案例(運用servlet傳輸數據)

public class TestServlet extends HttpServlet {

 

        /**

         * Constructor of the object.

         */

        publicTestServlet() {

               super();

        }

 

        public voiddoGet(HttpServletRequest request, HttpServletResponse response)

                       throwsServletException, IOException {

               doPost(request,response);

        }

        public voiddoPost(HttpServletRequest request, HttpServletResponse response)

                       throwsServletException, IOException {

 

               response.setContentType("text/html");

               PrintWriterout = response.getWriter();

            Stringdata="[{\"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 voidinit() throws ServletException {

               //Put your code here

        }

}
2.前台js請求處理數據代碼

function createXMLHttpRequest(){

        varxmlrequest;

        if(window.XMLHttpRequest){

               xmlrequest=newXMLHttpRequest();

        }elseif(window.ActiveXObject){

               try{

                       xmlrequest=newActiveXObject("Msxm12.XMLHTTP");

               }catch(e){

                       try{

                               xmlrequest=newActiveXObject("Microsoft.XMLHTTP");

                       }catch(e){

                               xmlrequest="";

                       }

               }

        }

        returnxmlrequest;

}

//獲取數據的函數

function change(){

        var xmlrequest=createXMLHttpRequest();

        xmlrequest.open("POST","TestServlet",true);

        xmlrequest.onreadystatechange=function(){

               if(xmlrequest.readyState==4&&xmlrequest.status==200){

                       vardata=JSON.parse(xmlrequest.responseText);

                       varcontent="<table border=1>";

                       for(vari=0;i<data.length;i++){

                               content+="<tr>";

                               for(oin data[i]){

                                      content+="<td>"+data[i][o]+"</td>";

                               }

                               content+="</tr>";

                       }

                       content+="</table>";

                       document.getElementById("test").innerHTML=content;

               }

        };

        xmlrequest.send();

}

總結:在用戶訪問網站整個生命周期中都會用到的數據用session來存儲,例如用戶名,登錄狀態,購物車信息.顯示在網頁上的信息數據大多通過 request或Ajax方式獲取


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM