JSP之Cookie

Cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器,通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复等。

首先创建index.jsp:

<%@page import="java.net.URLDecoder"%>
<%@page import="javax.activation.URLDataSource"%>
<%@ 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 '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 date="";
    if(cookies !=null){
    //遍历cookie对象集合
    for(int i=0;i<cookies.length;i++){
    if(cookies[i].getName().equals("cdp")){
    user=URLDecoder.decode(cookies[i].getValue().split("#")[0]);
    //获取用户名
    date=cookies[i].getValue().split("#")[1];
    //获取注册时间
        }
        }
        }
        
    if("".equals(user)&&"".equals(date)){
    //如果没有注册
    %>  
    <p>
    游客您好,欢迎初次光临!
    </p>   
    <form action="MyJsp.jsp method="post"></form>
     请输入姓名:<input name="user" type="text" value=""><br>
     <input type="submit" value="确定"> 
     <%
     }else{
     //已注册
      %>
      欢迎<b><%=user %>再次光临<br>
       你注册的时间是:<%=date %>
      <%
     }      
       %>

  </body>
</html>

再创建MyJsp.jsp,用于向cookie中写入对象注册信息:

<%@page import="java.net.URLEncoder"%>
<%@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 'MyJsp.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("GB18030");
    //设置请求的编译为GB18030
    String user=URLEncoder.encode(request.getParameter("user"),"utf-8");
    //获取用户名
    Cookie cookie=new Cookie("cdp",user+"#"+new java.util.Date().toLocaleString());
    //创建并实例化Cookie对象
    cookie.setMaxAge(60*60*24*30);
    //设置Cookie有效时间为30天
    response.addCookie(cookie);
    //保存Cookie
     %>
     <script type="text/javascript">window.location.href="index.jsp"</script>
  </body>
</html>
不努力,还要青春干什么?
原文地址:https://www.cnblogs.com/caidupingblogs/p/5233725.html