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>

 

posted @ 2019-05-30 15:07  南山下的采药人  阅读(794)  评论(0编辑  收藏  举报