com.interface21.web.util
Class WebUtils

java.lang.Object
  |
  +--com.interface21.web.util.WebUtils

public abstract class WebUtils
extends java.lang.Object

Miscellaneous utilities for web applications. Also used by various framework classes.

Author:
Rod Johnson, Juergen Hoeller

Field Summary
static java.lang.String DEFAULT_WEB_APP_ROOT_KEY
          Default web app root key: "webapp.root".
static java.lang.String HEADER_IFMODSINCE
          HTTP header value
static java.lang.String HEADER_LASTMOD
          HTTP header value
static java.lang.String SUBMIT_IMAGE_SUFFIX
          Name suffix in case of image buttons
static java.lang.String WEB_APP_ROOT_KEY_PARAM
          Web app root key parameter at the servlet context level (i.e. web.xml): "webAppRootKey".
 
Constructor Summary
WebUtils()
           
 
Method Summary
static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Retrieve the first cookie with the given name.
static java.lang.String getDirectoryForServletPath(java.lang.String servletPath)
          Given a servlet path string, determine the directory within the WAR this belongs to, ending with a /.
static java.lang.String getLookupPathForRequest(javax.servlet.http.HttpServletRequest request, boolean alwaysUseFullPath)
          Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application context.
static java.util.Properties getParametersStartingWith(javax.servlet.ServletRequest request, java.lang.String base)
          Convenience method to return a map from un-prefixed property names to values.
static java.lang.String getPathWithinApplication(javax.servlet.http.HttpServletRequest request)
          Return the path within the web application for the given request.
static java.lang.String getPathWithinServletMapping(javax.servlet.http.HttpServletRequest request)
          Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.
static java.lang.Object getSessionAttribute(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Check the given request for a session attribute of the given name.
static java.lang.String getUrlToApplication(javax.servlet.http.HttpServletRequest request)
          Return the URL of the root of the current application.
static boolean hasSubmitParameter(javax.servlet.ServletRequest request, java.lang.String name)
          Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x").
static void setSessionAttribute(javax.servlet.http.HttpServletRequest request, java.lang.String name, java.lang.Object value)
          Set the session attribute with the given name to the given value.
static void setWebAppRootSystemProperty(javax.servlet.ServletContext servletContext)
          Set a system property to the web application root directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WEB_APP_ROOT_KEY_PARAM

public static final java.lang.String WEB_APP_ROOT_KEY_PARAM
Web app root key parameter at the servlet context level (i.e. web.xml): "webAppRootKey".

DEFAULT_WEB_APP_ROOT_KEY

public static final java.lang.String DEFAULT_WEB_APP_ROOT_KEY
Default web app root key: "webapp.root".

HEADER_IFMODSINCE

public static final java.lang.String HEADER_IFMODSINCE
HTTP header value

HEADER_LASTMOD

public static final java.lang.String HEADER_LASTMOD
HTTP header value

SUBMIT_IMAGE_SUFFIX

public static final java.lang.String SUBMIT_IMAGE_SUFFIX
Name suffix in case of image buttons
Constructor Detail

WebUtils

public WebUtils()
Method Detail

setWebAppRootSystemProperty

public static void setWebAppRootSystemProperty(javax.servlet.ServletContext servletContext)
Set a system property to the web application root directory. The key of the system property can be defined with the "webAppRootKey" init parameter at the servlet context level (i.e. web.xml), the default key is "webapp.root".

Can be used for toolkits that support substition with system properties (i.e. System.getProperty values), like Log4J's ${key} syntax within log file locations.

Parameters:
servletContext - the servlet context of the web application
See Also:
WEB_APP_ROOT_KEY_PARAM, DEFAULT_WEB_APP_ROOT_KEY, WebAppRootListener

getSessionAttribute

public static java.lang.Object getSessionAttribute(javax.servlet.http.HttpServletRequest request,
                                                   java.lang.String name)
Check the given request for a session attribute of the given name. Returns null if there is no session or if the session has no such attribute. Does not create a new session if none has existed before!
Parameters:
request - current HTTP request
name - the name of the session attribute
Returns:
the value of the session attribute, or null if not found

setSessionAttribute

public static void setSessionAttribute(javax.servlet.http.HttpServletRequest request,
                                       java.lang.String name,
                                       java.lang.Object value)
Set the session attribute with the given name to the given value. Removes the session attribute if value is null, if a session existed at all. Does not create a new session on remove if none has existed before!
Parameters:
request - current HTTP request
name - the name of the session attribute

getCookie

public static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest request,
                                                  java.lang.String name)
Retrieve the first cookie with the given name. Note that multiple cookies can have the same name but different paths or domains.
Parameters:
name - cookie name
Returns:
the first cookie with the given name, or null if none is found

getUrlToApplication

public static java.lang.String getUrlToApplication(javax.servlet.http.HttpServletRequest request)
Return the URL of the root of the current application.
Parameters:
request - current HTTP request

getPathWithinApplication

public static java.lang.String getPathWithinApplication(javax.servlet.http.HttpServletRequest request)
Return the path within the web application for the given request.
Parameters:
request - current HTTP request
Returns:
the path within the web application

getPathWithinServletMapping

public static java.lang.String getPathWithinServletMapping(javax.servlet.http.HttpServletRequest request)
Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".

E.g.: servlet mapping = "/test"; request URI = "/test" -> "".

E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".

Parameters:
request - current HTTP request
Returns:
the path within the servlet mapping, or ""

getLookupPathForRequest

public static java.lang.String getLookupPathForRequest(javax.servlet.http.HttpServletRequest request,
                                                       boolean alwaysUseFullPath)
Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application context.
Parameters:
request - current HTTP request
alwaysUseFullPath - if the full path within the context should be used in any case
Returns:
the lookup path

getDirectoryForServletPath

public static java.lang.String getDirectoryForServletPath(java.lang.String servletPath)
Given a servlet path string, determine the directory within the WAR this belongs to, ending with a /. For example, /cat/dog/test.html would be returned as /cat/dog/. /test.html would be returned as /

getParametersStartingWith

public static java.util.Properties getParametersStartingWith(javax.servlet.ServletRequest request,
                                                             java.lang.String base)
Convenience method to return a map from un-prefixed property names to values. E.g. with a prefix of price, price_1, price_2 produce a properties object with mappings for 1, 2 to the same values.
Parameters:
request - HTTP request in which to look for parameters
base - beginning of parameter name (if this is null or the empty string, all parameters will match)
Returns:
properties mapping request parameters without the prefix

hasSubmitParameter

public static boolean hasSubmitParameter(javax.servlet.ServletRequest request,
                                         java.lang.String name)
Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x").
Parameters:
request - current HTTP request
name - name of the parameter
Returns:
if the parameter was sent
See Also:
SUBMIT_IMAGE_SUFFIX


Rod Johnson and Spring contributors 2001-2003.