Cookie

来源:互联网 发布:电脑笔记软件 知乎 编辑:程序博客网 时间:2024/05/29 14:33

cookie的简介

  • cookie是web服务器保存在客户端的一系列文本信息
  • cookie的作用:对特定对象的追踪、统计网页浏览次数、简单登录
  • 安全性能:榕溪信息泄露

cookie对象的常用方法:


实例:
  • 用户第一次登录时需要输入用户名和密码
  • 在20s内,无需再次登录则直接显示欢迎页面
思路:
  • 用户登陆后,创建cookie保存用户信息
  • 设置cookie的有效期为20s
  • 在登陆页循环遍历cookie数组,判断是否存在指定名称的cookie,若存在则直接跳转欢迎页面
cookie.jsp
<%@page import="java.net.URLEncoder"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'cookie.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <%    //创建cookie对象    //Cookie cusername = new Cookie("username","admin");    /**    Cookie中又只能存储英文,要想向cookie中存取中文就要对中文进行编码当向cookie中存储时,使用URLEncode类中的encode方法对文本进行转码。当从cookie中读取时,使用URLDecode类中的decode方法进行解码*/    Cookie cusername = new Cookie("username",URLEncoder.encode("东软","utf-8"));    Cookie cpassword = new Cookie("password","123");        //设置cookie存留时间    cusername.setMaxAge(10);    cpassword.setMaxAge(10);        //写入cookie    response.addCookie(cusername);    response.addCookie(cpassword);        //重定向cookie进行接收    response.sendRedirect("showcookie.jsp");     %>  </body></html>

showcookie.jsp
<%@page import="java.net.URLDecoder"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'showcookie.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <%      String name="";      String pass="";            //获得cookie数组      Cookie[] mycookie = request.getCookies();      for(int i = 0;i<mycookie.length;i++){        if(mycookie[i].getName().equals("username")){ name = mycookie[i].getValue();    //不是中文则不需要解码这行 name = URLDecoder.decode(name,"utf-8");            }        if(mycookie[i].getName().equals("password")){ pass = mycookie[i].getValue();               }      }            out.print("用户名:"+name+"<br>密码:"+pass);     %>  </body></html>

实例2:
  • 使用cookie,登录一个账户,若账户不存在则注册,若账户存在则在当前页面中显示欢迎页面
  • 要求cookie保存的时间可选
Login.jsp
<%@page import="java.net.URLEncoder"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'Login.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body><%    String username = "";    String password = "";    //获取当前站点的所有Cookie    Cookie[] cookies = request.getCookies();    for (int i = 0; i < cookies.length; i++) {//对cookies中的数据进行遍历,找到用户名、密码的数据        if ("username".equals(cookies[i].getName())) {            username = cookies[i].getValue();        }        if ("password".equals(cookies[i].getName())) {            password = cookies[i].getValue();        }    }    if(username.equals("")||password.equals("")){%> <form action="doLogin.jsp" method="post">姓名:<input type="text" name="username" /><br> 密码:<inputtype="password" name="password" /><br> 请选择日期:<select name="select"><option value="5">5s</option><option value="10">10s</option><option value="15">15s</option><option value="20">20s</option></select> <br><input type="submit" value="提交"></form><%  } else{ out.print("恭喜"+username+"登陆成功!"); }%> </body></html>

doLogin.jsp
<%@page import="java.net.URLDecoder"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'doLogin.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>    <body>    <%      String username = request.getParameter("username");      String password = request.getParameter("password");      if((username!=null)&&(password!=null)){      if((username.equals("admin"))&&(password.equals("123456"))){      int time =Integer.parseInt(request.getParameter("select"));      Cookie name = new Cookie("username",username);      Cookie pass = new Cookie("password",password);      name.setMaxAge(time);      pass.setMaxAge(time);      response.addCookie(name);      response.addCookie(pass);      response.sendRedirect("Login.jsp");      }else{          response.sendRedirect("Login.jsp");      }      }     %>  </body></html>


原创粉丝点击