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