Source code: com/aendvari/common/util/ServletUtil.java
1 /*
2 * ServletUtil.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.common.util;
11
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14 import javax.servlet.jsp.*;
15 import javax.servlet.jsp.tagext.*;
16
17
18 /**
19 * <p>Utility class for servlets.</p>
20 *
21 * @author Scott Milne
22 *
23 */
24
25 public class ServletUtil
26 {
27 /**
28 * Constants for <code>scope</code> searches. Related to
29 * <code>javax.servlet.jsp.PageContext</code> values.
30 *
31 * @author Scott Milne
32 *
33 */
34
35 public interface Scope
36 {
37 /** Specifies the PageContext.APPLICATION_SCOPE. */
38 final static String Application = "application";
39
40 /** Specifies the PageContext.PAGE_SCOPE. */
41 final static String Page = "page";
42
43 /** Specifies the PageContext.REQUEST_SCOPE. */
44 final static String Request = "request";
45
46 /** Specifies the PageContext.SESSION_SCOPE. */
47 final static String Session = "session";
48 }
49
50
51 /* Utilities */
52
53
54 /**
55 * Returns the specified attribute. Looks in the request and session attribute spaces.
56 *
57 * @param request The servlet request we are processing.
58 * @param name The name of the attribute.
59 *
60 * @return The object requested.
61 *
62 */
63
64 public static Object getAttribute(HttpServletRequest request, String name)
65 {
66 // check request
67 Object object = request.getAttribute(name);
68
69 // if null, then check session
70 if (object == null)
71 {
72 object = request.getSession().getAttribute(name);
73 }
74
75 return object;
76 }
77
78 /**
79 * <p>
80 * Uses <code>scope</code> for where to find the object wanted.
81 * If not found, an attempt to find the scope manually will occur.
82 * </p>
83 *
84 * @param pageContext The <code>pageContext</code> of the current JSP.
85 * @param scope The scope to search.
86 * @param attributeName The name of the attribute.
87 *
88 * @return The object (may be null) found in the scope.
89 *
90 */
91
92 public static Object getAttribute(PageContext pageContext, String scope, String attributeName)
93 {
94 Object attributeObject = null;
95
96 // if a scope was given, use it
97 if (scope != null)
98 {
99 // by default use the request scope
100 int inScope = PageContext.REQUEST_SCOPE;
101
102 // store the value where it's needed
103 if ((scope.compareToIgnoreCase(ServletUtil.Scope.Session)==0))
104 {
105 inScope = PageContext.SESSION_SCOPE;
106 }
107 else if ((scope.compareToIgnoreCase(ServletUtil.Scope.Page)==0))
108 {
109 inScope = PageContext.PAGE_SCOPE;
110 }
111 else if ((scope.compareToIgnoreCase(ServletUtil.Scope.Application)==0))
112 {
113 inScope = PageContext.APPLICATION_SCOPE;
114 }
115
116 // get the properties
117 attributeObject = pageContext.getAttribute(attributeName, inScope);
118 }
119 // try a search for the model tree
120 else
121 {
122 // try request first
123 attributeObject = pageContext.getAttribute(
124 attributeName, PageContext.REQUEST_SCOPE);
125
126 // try session
127 if (attributeObject == null)
128 {
129 attributeObject = pageContext.getAttribute(
130 attributeName, PageContext.SESSION_SCOPE);
131 }
132
133 // try page
134 if (attributeObject == null)
135 {
136 attributeObject = pageContext.getAttribute(
137 attributeName, PageContext.PAGE_SCOPE);
138 }
139
140 // try application
141 if (attributeObject == null)
142 {
143 attributeObject = pageContext.getAttribute(
144 attributeName, PageContext.APPLICATION_SCOPE);
145 }
146 }
147
148 return attributeObject;
149 }
150
151 /**
152 * <p>
153 * Places the supplied object in the <code>scope</code> specified.
154 * </p>
155 *
156 * @param pageContext The <code>pageContext</code> of the current JSP.
157 * @param scope The scope to search.
158 * @param attributeName The name of the attribute.
159 * @param attributeObject The attribute object.
160 *
161 */
162
163 public static void setAttribute(PageContext pageContext, String scope, String attributeName, Object attributeObject)
164 {
165 // by default use the request scope
166 int inScope = PageContext.REQUEST_SCOPE;
167
168 // if a scope was given, use it
169 if (scope != null)
170 {
171 // store the value where it's needed
172 if ((scope.compareToIgnoreCase(ServletUtil.Scope.Session)==0))
173 {
174 inScope = PageContext.SESSION_SCOPE;
175 }
176 else if ((scope.compareToIgnoreCase(ServletUtil.Scope.Page)==0))
177 {
178 inScope = PageContext.PAGE_SCOPE;
179 }
180 else if ((scope.compareToIgnoreCase(ServletUtil.Scope.Application)==0))
181 {
182 inScope = PageContext.APPLICATION_SCOPE;
183 }
184 }
185
186 // place the attribute
187 pageContext.setAttribute(attributeName, attributeObject, inScope);
188 }
189 }
190