web前后台数据交互的四种方式(转)

1.利用cookie对象 

Cookie是服务器保存在客户端中的一小段数据信息。使用Cookie有一个前提,就是客户端浏览器允许使用Cookie并对此做出相应的设置。一般不赞成使用Cookie。

(1)后台代码

1

2

Cookie cookie=new Cookie("name""hello"); 

response.addCookie(cookie);

 

(2)前台代码

 

1

2

3

4

5

6

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)后台代码

 

1

2

3

request.getSession().setAttribute("name", name); 

request.getSession().setMaxInactiveInterval(2); 

response.sendRedirect("welcome.jsp"); 

  

(2)前台代码(jsp页面)

1

Object user=request.getSession().getAttribute("name");

  

3.利用request重定向,设置setAttribute

 

(1)后台代码

 

1

2

request.setAttribute("name""cute"); 

request.getRequestDispatcher("welcome.jsp").forward(request, response);  //网址不会改变

 

PS:如果后台使用的转发代码为 response.sendRedirect("welcome.jsp");  //网址变为welcome.jsp

则request设置的参数无效,因为已经切换到另一个请求了,request参数的有效期为本次请求。

(2)前台代码

 

1

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

 

4.利用Ajax进行异步数据请求(得到的数据可以以json或xml格式返回,便于处理)

(1)后台代码案例(运用servlet传输数据)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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请求处理数据代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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方式获取

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值