测试cookie

登录页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<%
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 'index.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[] cookies = request.getCookies();                    //从request中获得Cookie对象的集合


      String user = "";                                         //登录用户
      
      String pwd = "";                                          //登录密码


      String date = "";                                         //注册的时间


  if (cookies != null) {


            for (int i = 0; i < cookies.length; i++) {         //遍历cookie对象的集合


                  if (cookies[i].getName().equals("mrCookie")) {             //如果cookie对象的名称为mrCookie


                        user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);     //获取用户名
                        pwd = URLDecoder.decode(cookies[i].getValue().split("#")[1]);     //获取用户名
                        date = cookies[i].getValue().split("#")[2];                              //获取注册时间


}


            }


      }


      if ("".equals(user) && "".equals(date)&&"".equals(pwd)) {                                                        //如果没有注册


%>
  游客您好,欢迎您初次光临!


            <form action="deal.jsp" method="post">


                  请输入姓名:<input name="user" type="text" value=""><br/>
                  请输入密码:<input name="pwd" type="text" value="">


                  <input type="submit" value="确定">


            </form>


<%


      } else {                                                                                 //已经注册


%>


            欢迎[<b><%=user %></b>]再次光临<br>
            密码是:<%=pwd %>


            您注册的时间是:<%=date %>


<%


      }


%>


  </body>
</html>


处理页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>




<%
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 'deal.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>
    <%


request.setCharacterEncoding("utf-8");                                            //设置请求的编译为GB18030


String user=URLEncoder.encode(request.getParameter("user"));                  //获取用户名


String pwd=URLEncoder.encode(request.getParameter("pwd"),"utf-8");                  //获取用户名


Cookie cookie = new Cookie("mrCookie", user+"#"+pwd+"#"+new java.util.Date().toLocaleString());//创建并实例化cookie对象


cookie.setMaxAge(60*60*24*30);                                                        //设置cookie有效期30天
    response.addCookie(cookie);                                                  //保存cookie


%>


   
  </body>
</html>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值