1 18 19 20 package org.apache.struts.taglib.bean; 21 22 23 import java.util.ArrayList ; 24 import javax.servlet.http.Cookie ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 import org.apache.struts.util.MessageResources; 29 import org.apache.struts.taglib.TagUtils; 30 31 32 38 39 public class CookieTag extends TagSupport { 40 41 42 44 45 49 protected String id = null; 50 51 public String getId() { 52 return (this.id); 53 } 54 55 public void setId(String id) { 56 this.id = id; 57 } 58 59 60 63 protected static MessageResources messages = 64 MessageResources.getMessageResources 65 ("org.apache.struts.taglib.bean.LocalStrings"); 66 67 68 71 protected String multiple = null; 72 73 public String getMultiple() { 74 return (this.multiple); 75 } 76 77 public void setMultiple(String multiple) { 78 this.multiple = multiple; 79 } 80 81 82 85 protected String name = null; 86 87 public String getName() { 88 return (this.name); 89 } 90 91 public void setName(String name) { 92 this.name = name; 93 } 94 95 96 99 protected String value = null; 100 101 public String getValue() { 102 return (this.value); 103 } 104 105 public void setValue(String value) { 106 this.value = value; 107 } 108 109 110 112 113 118 public int doStartTag() throws JspException { 119 120 ArrayList values = new ArrayList (); 122 Cookie cookies[] = 123 ((HttpServletRequest ) pageContext.getRequest()).getCookies(); 124 if (cookies == null) 125 cookies = new Cookie [0]; 126 127 for (int i = 0; i < cookies.length; i++) { 128 if (name.equals(cookies[i].getName())) 129 values.add(cookies[i]); 130 } 131 if ((values.size() < 1) && (value != null)) 132 values.add(new Cookie (name, value)); 133 if (values.size() < 1) { 134 JspException e = new JspException 135 (messages.getMessage("cookie.get", name)); 136 TagUtils.getInstance().saveException(pageContext, e); 137 throw e; 138 } 139 140 if (multiple == null) { 142 Cookie cookie = (Cookie ) values.get(0); 143 pageContext.setAttribute(id, cookie); 144 } else { 145 cookies = new Cookie [values.size()]; 146 pageContext.setAttribute(id, values.toArray(cookies)); 147 } 148 return (SKIP_BODY); 149 150 } 151 152 153 156 public void release() { 157 158 super.release(); 159 id = null; 160 multiple = null; 161 name = null; 162 value = null; 163 164 } 165 166 167 } 168 | Popular Tags |