CSDN博客

img Inber

一个操作cookie的javabean

发表于2004/7/8 12:10:00  1343人阅读

分类: java收藏

一个操作cookie的javabean
作者:海仔 时间:2003-4-22

package org.rautinee.util;

import java.io.*;
import java.net.*;
import java.beans.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class MagCookie extends HttpServlet{

/*
添加一个cookie
cookieName -- cookie的名称
cookieValue -- cookie的值
*/
public void addCookie(HttpServletResponse res, String cookieName, String cookieValue,int cookieMaxAge)
{
try {
Cookie c = new Cookie(cookieName, cookieValue);
c.setMaxAge(cookieMaxAge);
res.addCookie(c);
}
catch (Exception ex) {
System.err.print(ex.getMessage());
}

}
public String getCookieValue(HttpServletRequest req,String cookieName)
{
String cookieValue="";
try {
Cookie c[]=req.getCookies();
for (int n=0;n<c.length;n++)
{
if (c[n].getName().equals(cookieName))
{
cookieValue=c[n].getValue();

}
}
}
catch (Exception ex) {
System.err.print(ex.getMessage());
}
return cookieValue;

}


}
0 0

相关博文

我的热门文章

img
取 消
img