1 16 17 package javax.servlet.jsp.jstl.core; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.ServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 import javax.servlet.jsp.PageContext ; 23 24 27 public class Config { 28 29 32 33 37 public static final String FMT_LOCALE 38 = "javax.servlet.jsp.jstl.fmt.locale"; 39 40 43 public static final String FMT_FALLBACK_LOCALE 44 = "javax.servlet.jsp.jstl.fmt.fallbackLocale"; 45 46 49 public static final String FMT_LOCALIZATION_CONTEXT 50 = "javax.servlet.jsp.jstl.fmt.localizationContext"; 51 52 55 public static final String FMT_TIME_ZONE 56 = "javax.servlet.jsp.jstl.fmt.timeZone"; 57 58 61 62 65 public static final String SQL_DATA_SOURCE 66 = "javax.servlet.jsp.jstl.sql.dataSource"; 67 68 72 public static final String SQL_MAX_ROWS 73 = "javax.servlet.jsp.jstl.sql.maxRows"; 74 75 78 private static final String PAGE_SCOPE_SUFFIX = ".page"; 79 private static final String REQUEST_SCOPE_SUFFIX = ".request"; 80 private static final String SESSION_SCOPE_SUFFIX = ".session"; 81 private static final String APPLICATION_SCOPE_SUFFIX = ".application"; 82 83 99 public static Object get(PageContext pc, String name, int scope) { 100 switch (scope) { 101 case PageContext.PAGE_SCOPE: 102 return pc.getAttribute(name + PAGE_SCOPE_SUFFIX, scope); 103 case PageContext.REQUEST_SCOPE: 104 return pc.getAttribute(name + REQUEST_SCOPE_SUFFIX, scope); 105 case PageContext.SESSION_SCOPE: 106 return get(pc.getSession(), name); 107 case PageContext.APPLICATION_SCOPE: 108 return pc.getAttribute(name + APPLICATION_SCOPE_SUFFIX, scope); 109 default: 110 throw new IllegalArgumentException ("unknown scope"); 111 } 112 } 113 114 128 public static Object get(ServletRequest request, String name) { 129 return request.getAttribute(name + REQUEST_SCOPE_SUFFIX); 130 } 131 132 147 public static Object get(HttpSession session, String name) { 148 Object ret = null; 149 if (session != null) { 150 try { 151 ret = session.getAttribute(name + SESSION_SCOPE_SUFFIX); 152 } catch (IllegalStateException ex) {} } 154 return ret; 155 } 156 157 171 public static Object get(ServletContext context, String name) { 172 return context.getAttribute(name + APPLICATION_SCOPE_SUFFIX); 173 } 174 175 188 public static void set(PageContext pc, String name, Object value, 189 int scope) { 190 switch (scope) { 191 case PageContext.PAGE_SCOPE: 192 pc.setAttribute(name + PAGE_SCOPE_SUFFIX, value, scope); 193 break; 194 case PageContext.REQUEST_SCOPE: 195 pc.setAttribute(name + REQUEST_SCOPE_SUFFIX, value, scope); 196 break; 197 case PageContext.SESSION_SCOPE: 198 pc.setAttribute(name + SESSION_SCOPE_SUFFIX, value, scope); 199 break; 200 case PageContext.APPLICATION_SCOPE: 201 pc.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value, scope); 202 break; 203 default: 204 throw new IllegalArgumentException ("unknown scope"); 205 } 206 } 207 208 221 public static void set(ServletRequest request, String name, Object value) { 222 request.setAttribute(name + REQUEST_SCOPE_SUFFIX, value); 223 } 224 225 238 public static void set(HttpSession session, String name, Object value) { 239 session.setAttribute(name + SESSION_SCOPE_SUFFIX, value); 240 } 241 242 255 public static void set(ServletContext context, String name, Object value) { 256 context.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value); 257 } 258 259 272 public static void remove(PageContext pc, String name, int scope) { 273 switch (scope) { 274 case PageContext.PAGE_SCOPE: 275 pc.removeAttribute(name + PAGE_SCOPE_SUFFIX, scope); 276 break; 277 case PageContext.REQUEST_SCOPE: 278 pc.removeAttribute(name + REQUEST_SCOPE_SUFFIX, scope); 279 break; 280 case PageContext.SESSION_SCOPE: 281 pc.removeAttribute(name + SESSION_SCOPE_SUFFIX, scope); 282 break; 283 case PageContext.APPLICATION_SCOPE: 284 pc.removeAttribute(name + APPLICATION_SCOPE_SUFFIX, scope); 285 break; 286 default: 287 throw new IllegalArgumentException ("unknown scope"); 288 } 289 } 290 291 302 public static void remove(ServletRequest request, String name) { 303 request.removeAttribute(name + REQUEST_SCOPE_SUFFIX); 304 } 305 306 317 public static void remove(HttpSession session, String name) { 318 session.removeAttribute(name + SESSION_SCOPE_SUFFIX); 319 } 320 321 332 public static void remove(ServletContext context, String name) { 333 context.removeAttribute(name + APPLICATION_SCOPE_SUFFIX); 334 } 335 336 354 public static Object find(PageContext pc, String name) { 355 Object ret = get(pc, name, PageContext.PAGE_SCOPE); 356 if (ret == null) { 357 ret = get(pc, name, PageContext.REQUEST_SCOPE); 358 if (ret == null) { 359 if (pc.getSession() != null) { 360 ret = get(pc, name, PageContext.SESSION_SCOPE); 362 } 363 if (ret == null) { 364 ret = get(pc, name, PageContext.APPLICATION_SCOPE); 365 if (ret == null) { 366 ret = pc.getServletContext().getInitParameter(name); 367 } 368 } 369 } 370 } 371 372 return ret; 373 } 374 } 375 | Popular Tags |