session cookie

Cookie(客户端,不是内置对象):Cookie是由服务端产生的,再发给客户端保存。

相当于本地缓存的作用:客户端(请求) --> 服务端(MP4)–> 客户端(MP4)。这种技术虽然方便,但是如果放用户名、密码会不安全。

key(name)=value 键值对,它是由javax.servlet.http.Cookie类产生的。

Cookie常见函数:
public Cookie(String name, String value)
String getName():获取name
String getValue():获取value
void setMaxAge(int expiry):设置最大有效期(秒)。

服务端准备cookie:
response.addCookie(cookie)(产生)
页面跳转(转发,重定向)
客户端获取cookie:request.getCookies();

a.服务端增加cookie:response对象,客户端获取cookie:request对象
b.不能直接获取某一个单独对象,只能一次性将全部的cookie拿到

模拟cookie获取
服务端():
在这里插入图片描述
客户端:
在这里插入图片描述
在这里插入图片描述
addcookie.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Cookie cookie1 =new Cookie("name","zs");
        Cookie cookie2 =new Cookie("pwd","123");
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        response.sendRedirect("result.jsp");
    %>
</body>
</html>

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Cookie[] cookies=request.getCookies();
        for (Cookie cookie:cookies){
            out.print(cookie.getName()+"<br>"+"--"+cookie.getValue()+"<br>");
        }
    %>
</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值