javaBean: Class aa{ public getCookie(HttpServletRequest request, HttpServletResponse response){ ...... } } jsp页面: new aa().getCookie(request,response);
Take this:
/** * Returns the specified cookie, or <code>null</code> if the cookie * does not exist. * * @param request The HttpServletRequest object, known as "request" in a * JSP page. * @param name the name of the cookie. * @return the Cookie object if it exists, otherwise <code>null</code>. */ public static Cookie getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); // Return null if there are no cookies or the name is invalid. if(cookies == null || name == null || name.length() == 0) { return null; } // Otherwise, we do a linear scan for the cookie. for (int i = 0; i < cookies.length; i++) { if(cookies[i].getName().equals(name) ) { return cookies[i]; } } return null; } public static void saveCookie(HttpServletResponse response, String name, String value) { // Save the cookie value for 1 month saveCookie(response, name, value, 60*60*24*30); } /** * Stores a value in a cookie. This cookie will persist for the amount * specified in the <tt>saveTime</tt> parameter. * * @param request The HttpServletResponse object, known as "response" in a * JSP page. * @param name a name to identify the cookie * @param value the value to store in the cookie * @param saveTime the time (in seconds) this cookie should live */ public static void saveCookie(HttpServletResponse response, String name, String value, int saveTime) { // Check to make sure the new value is not null (appservers like Tomcat // 4 blow up if the value is null). if (value == null) { value = ""; } Cookie cookie = new Cookie(name, value); cookie.setMaxAge(saveTime); cookie.setPath("/"); response.addCookie(cookie); }
Class aa{
public getCookie(HttpServletRequest request,
HttpServletResponse response){
......
}
}
jsp页面:
new aa().getCookie(request,response);
/**
* Returns the specified cookie, or <code>null</code> if the cookie
* does not exist.
*
* @param request The HttpServletRequest object, known as "request" in a
* JSP page.
* @param name the name of the cookie.
* @return the Cookie object if it exists, otherwise <code>null</code>.
*/
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie cookies[] = request.getCookies();
// Return null if there are no cookies or the name is invalid.
if(cookies == null || name == null || name.length() == 0) {
return null;
}
// Otherwise, we do a linear scan for the cookie.
for (int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals(name) ) {
return cookies[i];
}
}
return null;
}
public static void saveCookie(HttpServletResponse response, String name,
String value)
{
// Save the cookie value for 1 month
saveCookie(response, name, value, 60*60*24*30);
} /**
* Stores a value in a cookie. This cookie will persist for the amount
* specified in the <tt>saveTime</tt> parameter.
*
* @param request The HttpServletResponse object, known as "response" in a
* JSP page.
* @param name a name to identify the cookie
* @param value the value to store in the cookie
* @param saveTime the time (in seconds) this cookie should live
*/
public static void saveCookie(HttpServletResponse response, String name,
String value, int saveTime)
{
// Check to make sure the new value is not null (appservers like Tomcat
// 4 blow up if the value is null).
if (value == null) {
value = "";
}
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(saveTime);
cookie.setPath("/");
response.addCookie(cookie);
}