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>

JSP之Cookie的更多相关文章

  1. jsp中cookie的一个报错

    写项目时在一个jsp页面中使用了cookie,用逗号分隔的数据,服务器老报错,搞了一个小时,终于清楚了,jsp的cookie中不能使用逗号. cookie规则:这个规则用于jsp.asp中(下面这两句 ...

  2. jsp 入门 cookie session

    Java Server Page ==> 服务器端的动态页面资源.用来做显示的功能. JSP构成 ==> HTML 脚本代码 标签构成. JSP 原理 ==> 实际上就是 servl ...

  3. jsp利用cookie记住用户名,下次登录时显示在文本框中(仅仅一个Cookie就整了将近三个小时,⊙﹏⊙b汗)

    <%@page import="java.net.URLDecoder"%> <%@page import="sun.security.util.Len ...

  4. 【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能

    Cookie是服务器存放在客户端的一些数据,比如密码,以及你曾经访问过的一些数据. 设置Cookie //设置cookie Cookie cookie = new Cookie("TOM&q ...

  5. JSP获取Cookie对象

    cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器的.通过使用cookie可以标识用户身份,记录用户和密码,跟踪重复用户等.浏览器将cookie以key/value的形式保存到客户机的某 ...

  6. JSP之Cookie的实现

    在我们浏览网页的时候,经常会看到自己曾经浏览过的网页的具体的一些信息,那这些究竟是通过什么来实现的呢?难道是有人在监视我们的电脑吗?其实不是的,实现这一功能就是利用了我们接下来看到的cookie技术. ...

  7. jsp使用cookie自动登录

    Login.jsp <%@ page language="java" import="java.util.*" pageEncoding="ut ...

  8. JSP(4)—Cookie创建及简单案例(自动登录)

    Cookie的创建: 创建一个JSP页面,第一次访问时显示没有Cookie,正在创建,再次访问就会自动显示cookie的名称,并设置cookie过期时间 <% //在javaweb规范中使用Co ...

  9. JSP(3)—Cookie和Session

    HTTP是一个无状态的协议,web服务器无法分辨出那些请求是同一个浏览器发出的,浏览器每一次请求都是孤立的 即使HTTP1.1支持持续链接,但当用户有一段时间没有请求时,连接也会关闭. 如何实现网上的 ...

随机推荐

  1. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  2. C# Dictionary用法总结

    转自:http://www.cnblogs.com/linlf03/archive/2011/12/09/2282574.html http://www.cnblogs.com/linzheng/ar ...

  3. Spring Batch Concepts Chapter

    Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...

  4. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C. Letter 暴力

    C. Letter Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/180/problem/C D ...

  5. android短信发送器源代码

    Activity类: import java.util.List;import android.app.Activity;import android.app.PendingIntent;import ...

  6. iOS strong 和weak的形象理解

    转自:http://hi.baidu.com/phone_lwc/item/c36e5bfe1cf9c313ce9f32be 觉得讲的很容易理解 The difference is that an o ...

  7. cocos2d-x使用tinyxml2存储解析xml

    我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库. 导入头文件:#include "support/tinyxml2/tinyxml2.h" using nam ...

  8. Ng-include 例子

    <body> <div ng-app="myApp"> <div ng-controller="firstController"& ...

  9. string2array($value);

    /*** 将字符串转换为数组** @param string $data 字符串* @return array 返回数组格式,如果,data为空,则返回空数组*/if ( !function_exis ...

  10. php执行效率相关的语句

    一:字符替换: strtr > str_replace > preg_replace 注意: 1:一般用strtr函数的这种形式:string strtr ( string $str , ...