使用javascript及java对Cookie的读写
2007-08-13 16:14
471 查看
1、javascript读取cookie
操作步骤:
1):取得当前网站的所有COOKIE,确定长度是否大于0。
2):如果大于0,再看要查找的COOKIE名是否在取得的字符串中。
3):如果在,取得该(COOKIE名+其长度+等号)对应的位置,作为起始位置;
4):从该起始位置,找到第一们“;”所在的位置。
5):取中间的内容,就是需要的COOKIE值。
//读取Cookie的函数
function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";",offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
//这个很简单,直接使用document.cookie等于(COOKIE名等于对应的值)就OK
//写入Cookie的函数
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
return cookieValue;
}
2、Java读取COOKIE
首先要通过Cookie[] cookies=request.getCookies();取得所有的COOKIE,然后使用下面的函数取得需要的COOKIE值,没有就返回null
//取得COOKIE
public String getCookieValue(Cookie[] cookies, String cookieName) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
return (cookie.getValue());
}
}
return null;
}
操作步骤:
1):取得当前网站的所有COOKIE,确定长度是否大于0。
2):如果大于0,再看要查找的COOKIE名是否在取得的字符串中。
3):如果在,取得该(COOKIE名+其长度+等号)对应的位置,作为起始位置;
4):从该起始位置,找到第一们“;”所在的位置。
5):取中间的内容,就是需要的COOKIE值。
//读取Cookie的函数
function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";",offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
//这个很简单,直接使用document.cookie等于(COOKIE名等于对应的值)就OK
//写入Cookie的函数
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
return cookieValue;
}
2、Java读取COOKIE
首先要通过Cookie[] cookies=request.getCookies();取得所有的COOKIE,然后使用下面的函数取得需要的COOKIE值,没有就返回null
//取得COOKIE
public String getCookieValue(Cookie[] cookies, String cookieName) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
return (cookie.getValue());
}
}
return null;
}
相关文章推荐
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- 在JAVASCRIPT中怎么使用COOKIE?
- java使用POI jar包读写xls文件
- Flex通过javascript读写cookie-可行
- Java中对Cookie的使用
- Java cookie的使用
- javascript中cookie使用--登录记住密码
- java中Cookie的使用
- java使用cookie实现下次自动登录
- 使用JAVA EXCEL API(jxl.jar)读写excel文件
- 了解URL编码的基本概念,在javascript和java程序中使用内置的API进行编码和解码
- java中Cookie的使用
- java中Cookie的使用
- Java网络编程从入门到精通(34):读写缓冲区中的数据---使用get和put方法按顺序读写单个数据
- Java Cookie的使用以及一个小案例
- Java使用RandomAccessFile读写文件
- 使用POI实现Java读写Excel文件
- Java与Javascript交互(使用Protobuf协议)
- JAVA序列化在IO中读写对象的使用