一个操作cookie的javabean

一个操作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;

}


}

你可能感兴趣的:(java,C++,c,servlet,C#)