org.springframework.web.util
Class WebUtils

java.lang.Object
  extended byorg.springframework.web.util.WebUtils

public abstract class WebUtils
extends Object

Miscellaneous utilities for web applications. Used by various framework classes.

Author:
Rod Johnson, Juergen Hoeller

Field Summary
static String DEFAULT_CHARACTER_ENCODING
          Default character encoding to use when request.getCharacterEncoding returns null, according to the Servlet spec.
static String DEFAULT_WEB_APP_ROOT_KEY
          Default web app root key: "webapp.root"
static String HTML_ESCAPE_CONTEXT_PARAM
          HTML escape parameter at the servlet context level (i.e. a context-param in web.xml): "defaultHtmlEscape".
static String[] SUBMIT_IMAGE_SUFFIXES
          Name suffixes in case of image buttons
static String TEMP_DIR_CONTEXT_ATTRIBUTE
          Standard Servlet spec context attribute that specifies a temporary directory for the current web application, of type java.io.File
static String WEB_APP_ROOT_KEY_PARAM
          Web app root key parameter at the servlet context level (i.e. a context-param in web.xml): "webAppRootKey".
 
Constructor Summary
WebUtils()
           
 
Method Summary
static void exposeRequestAttributes(ServletRequest request, Map attributes)
          Expose the given Map as request attributes, using the keys as attribute names and the values as corresponding attribute values.
static Cookie getCookie(HttpServletRequest request, String name)
          Retrieve the first cookie with the given name.
static Object getOrCreateSessionAttribute(HttpSession session, String name, Class clazz)
          Get the specified session attribute, creating and setting a new attribute if no existing found.
static Map getParametersStartingWith(ServletRequest request, String prefix)
          Return a map containing all parameters with the given prefix.
static String getRealPath(ServletContext servletContext, String path)
          Return the real path of the given path within the web application, as provided by the servlet container.
static Object getRequiredSessionAttribute(HttpServletRequest request, String name)
          Check the given request for a session attribute of the given name.
static Object getSessionAttribute(HttpServletRequest request, String name)
          Check the given request for a session attribute of the given name.
static String getSessionId(HttpServletRequest request)
          Determine the session id of the given request, if any.
static File getTempDir(ServletContext servletContext)
          Return the temporary directory for the current web application, as provided by the servlet container.
static boolean hasSubmitParameter(ServletRequest request, 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" or name + ".y").
static boolean isDefaultHtmlEscape(ServletContext servletContext)
          Return whether default HTML escaping is enabled for the web application, i.e. the value of the "defaultHtmlEscape" context-param in web.xml (if any).
static void removeWebAppRootSystemProperty(ServletContext servletContext)
          Remove the system property that points to the web app root directory.
static void setSessionAttribute(HttpServletRequest request, String name, Object value)
          Set the session attribute with the given name to the given value.
static void setWebAppRootSystemProperty(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

DEFAULT_CHARACTER_ENCODING

public static final String DEFAULT_CHARACTER_ENCODING
Default character encoding to use when request.getCharacterEncoding returns null, according to the Servlet spec.

See Also:
ServletRequest.getCharacterEncoding(), Constant Field Values

TEMP_DIR_CONTEXT_ATTRIBUTE

public static final String TEMP_DIR_CONTEXT_ATTRIBUTE
Standard Servlet spec context attribute that specifies a temporary directory for the current web application, of type java.io.File

See Also:
Constant Field Values

HTML_ESCAPE_CONTEXT_PARAM

public static final String HTML_ESCAPE_CONTEXT_PARAM
HTML escape parameter at the servlet context level (i.e. a context-param in web.xml): "defaultHtmlEscape".

See Also:
Constant Field Values

WEB_APP_ROOT_KEY_PARAM

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

See Also:
Constant Field Values

DEFAULT_WEB_APP_ROOT_KEY

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

See Also:
Constant Field Values

SUBMIT_IMAGE_SUFFIXES

public static final String[] SUBMIT_IMAGE_SUFFIXES
Name suffixes in case of image buttons

Constructor Detail

WebUtils

public WebUtils()
Method Detail

setWebAppRootSystemProperty

public static void setWebAppRootSystemProperty(ServletContext servletContext)
                                        throws IllegalStateException
Set a system property to the web application root directory. The key of the system property can be defined with the "webAppRootKey" context-param in web.xml. Default is "webapp.root".

Can be used for tools that support substition with System.getProperty values, like Log4J's "${key}" syntax within log file locations.

Parameters:
servletContext - the servlet context of the web application
Throws:
IllegalStateException - if the system property is already set, or if the WAR file is not expanded
See Also:
WEB_APP_ROOT_KEY_PARAM, DEFAULT_WEB_APP_ROOT_KEY, WebAppRootListener, Log4jWebConfigurer

removeWebAppRootSystemProperty

public static void removeWebAppRootSystemProperty(ServletContext servletContext)
Remove the system property that points to the web app root directory. To be called on shutdown of the web application.

Parameters:
servletContext - the servlet context of the web application
See Also:
setWebAppRootSystemProperty(javax.servlet.ServletContext)

isDefaultHtmlEscape

public static boolean isDefaultHtmlEscape(ServletContext servletContext)
Return whether default HTML escaping is enabled for the web application, i.e. the value of the "defaultHtmlEscape" context-param in web.xml (if any).

Parameters:
servletContext - the servlet context of the web application
Returns:
whether default HTML escaping is enabled (default is false)

getTempDir

public static File getTempDir(ServletContext servletContext)
Return the temporary directory for the current web application, as provided by the servlet container.

Parameters:
servletContext - the servlet context of the web application
Returns:
the File representing the temporary directory

getRealPath

public static String getRealPath(ServletContext servletContext,
                                 String path)
                          throws FileNotFoundException
Return the real path of the given path within the web application, as provided by the servlet container.

Prepends a slash if the path does not already start with a slash, and throws a FileNotFoundException if the path cannot be resolved to a resource (in contrast to ServletContext's getRealPath, which returns null).

Parameters:
servletContext - the servlet context of the web application
Returns:
the corresponding real path
Throws:
FileNotFoundException - if the path cannot be resolved to a resource
See Also:
ServletContext.getRealPath(java.lang.String)

getSessionId

public static String getSessionId(HttpServletRequest request)
Determine the session id of the given request, if any.

Parameters:
request - current HTTP request
Returns:
the session id, or null if none

getSessionAttribute

public static Object getSessionAttribute(HttpServletRequest request,
                                         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

getRequiredSessionAttribute

public static Object getRequiredSessionAttribute(HttpServletRequest request,
                                                 String name)
                                          throws IllegalStateException
Check the given request for a session attribute of the given name. Throws an exception 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
Throws:
IllegalStateException - if the session attribute could not be found

setSessionAttribute

public static void setSessionAttribute(HttpServletRequest request,
                                       String name,
                                       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 if not necessary!

Parameters:
request - current HTTP request
name - the name of the session attribute

getOrCreateSessionAttribute

public static Object getOrCreateSessionAttribute(HttpSession session,
                                                 String name,
                                                 Class clazz)
                                          throws IllegalArgumentException
Get the specified session attribute, creating and setting a new attribute if no existing found. The given class needs to have a public no-arg constructor. Useful for on-demand state objects in a web tier, like shopping carts.

Parameters:
session - current HTTP session
name - the name of the session attribute
clazz - the class to instantiate for a new attribute
Returns:
the value of the session attribute, newly created if not found
Throws:
IllegalArgumentException - if the session attribute could not be instantiated

exposeRequestAttributes

public static void exposeRequestAttributes(ServletRequest request,
                                           Map attributes)
                                    throws IllegalArgumentException
Expose the given Map as request attributes, using the keys as attribute names and the values as corresponding attribute values. Keys need to be Strings.

Parameters:
request - current HTTP request
attributes - the attributes Map
Throws:
IllegalArgumentException - if an invalid key is found in the Map

getCookie

public static Cookie getCookie(HttpServletRequest request,
                               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

hasSubmitParameter

public static boolean hasSubmitParameter(ServletRequest request,
                                         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" or name + ".y").

Parameters:
request - current HTTP request
name - name of the parameter
Returns:
if the parameter was sent
See Also:
SUBMIT_IMAGE_SUFFIXES

getParametersStartingWith

public static Map getParametersStartingWith(ServletRequest request,
                                            String prefix)
Return a map containing all parameters with the given prefix. Maps single values to String and multiple values to String array.

For example, with a prefix of "spring_", "spring_param1" and "spring_param2" result in a Map with "param1" and "param2" as keys.

Similar to Servlet 2.3's ServletRequest.getParameterMap, but more flexible and compatible with Servlet 2.2.

Parameters:
request - HTTP request in which to look for parameters
prefix - the beginning of parameter names (if this is null or the empty string, all parameters will match)
Returns:
map containing request parameters without the prefix, containing either a String or a String array as values
See Also:
ServletRequest.getParameterNames(), ServletRequest.getParameterValues(java.lang.String), ServletRequest.getParameterMap()


Copyright (C) 2003-2004 The Spring Framework Project.