Cookie在Java中的使用案例

Cookie译为小型文本文件或小甜饼,Web应用程序利用Cookie在客户端缓存服务器端文件。Cookie是以键值对形式存储在客户端主机硬盘中,由服务器端发送给客户端,客户端再下一次访问服务器端时,服务器端可以获取到客户端Cookie缓存文件。

Cookie是由服务器端创建的,然后由服务器端发送给客户端,客户端以键值对形式存储Cookie,并标注Cookie的来源。客户端再次访问服务器端时,存储的Cookie会保存在请求协议中,服务器端可以获取上次存储的缓存文件内容。

1.doLogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String ck = request.getParameter("save");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        if("admin".equals(name)&&"123456".equals(pwd)){
            if(ck!=null){
                //添加cookie
                Cookie cookie_name = new Cookie("name",name);
                Cookie cookie_pwd = new Cookie("pwd",pwd);
                //设置Cookie的时效, 设置了时效的cookie 会保存在硬盘上,只要在时效内,不同的浏览器都可以获取到该cookie对象
                cookie_name.setMaxAge(60*60*24);
                cookie_pwd.setMaxAge(60*60*24);
                response.addCookie(cookie_name);
                response.addCookie(cookie_pwd);
            }
                response.sendRedirect("B.jsp");
        }else{
            response.sendRedirect("login.jsp");
        }
    %>
</body>
</html>

2.B.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
    //获取
    Object num = application.getAttribute("num");
    int number = 0;
    if(num==null){
        //第一次访问
        number=1;
    }else{
        //已经被访问
        number=(Integer)num;
        number++;
    }
    application.setAttribute("num", number);
    %>
    <h1>你是第:<%=number %></h1>
</body>
</html>

3.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String name="";
        String pwd="";
        //获取cookie,每一次浏览器与客户端的回话会产生请求 request,cookies 对象即为所有当前会话的cookie
        Cookie [] cookies=request.getCookies();
        for(int i=0;i<cookies.length;i++){
            if("name".equals(cookies[i].getName())){
                name=cookies[i].getValue();
            }
            if("pwd".equals(cookies[i].getName())){
                pwd=cookies[i].getValue();
            }
        }
    %>
    <form action="doLogin.jsp" method="post">
        用户名:<input name="name"></br>
        密码:<input name="pwd" type="password"></br>
        是否记录用户名密码:<input type="checkbox" name="save"></br>
        <input type="submit" value="登陆">
    </form>
</body>
</html>

 

转载于:https://www.cnblogs.com/jiangaihu/p/10949642.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值