html cookie数据交互,web前后台数据交互的四种方式

本文详细介绍了在Java Web开发中如何使用Cookie、Session以及Ajax进行数据交互。通过实例代码展示了如何在后台设置Cookie和Session,以及在前台如何获取和使用这些数据。同时,还探讨了使用Ajax进行异步数据请求的方法,包括后端Servlet的数据传输和前端JavaScript的响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.

*

* @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="

for(var i=0;i

content+="

";

for(o in data[i]){

content+="

"+data[i][o]+"";

}

content+="

";

}

content+="

";

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

}

};

xmlrequest.send();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值