1 16 17 package org.apache.velocity.tools.view.tools; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.http.Cookie ; 23 import org.apache.velocity.tools.view.context.ViewContext; 24 25 47 public class CookieTool implements ViewTool 48 { 49 50 protected HttpServletRequest request; 51 protected HttpServletResponse response; 52 53 54 59 public void init(Object obj) 60 { 61 ViewContext context = (ViewContext)obj; 62 this.request = context.getRequest(); 63 this.response = context.getResponse(); 64 } 65 66 67 74 public Cookie [] getAll() 75 { 76 return request.getCookies(); 77 } 78 79 80 87 public Cookie get(String name) 88 { 89 Cookie [] all = getAll(); 90 if (all == null) 91 { 92 return null; 93 } 94 95 for (int i = 0; i < all.length; i++) 96 { 97 Cookie cookie = all[i]; 98 if (cookie.getName().equals(name)) 99 { 100 return cookie; 101 } 102 } 103 return null; 104 } 105 106 107 115 public void add(String name, String value) 116 { 117 response.addCookie(new Cookie (name, value)); 118 } 119 120 121 129 public void add(String name, String value, int maxAge) 130 { 131 132 Cookie c = new Cookie (name, value); 133 c.setMaxAge(maxAge); 134 response.addCookie(c); 135 } 136 137 } 138 | Popular Tags |