org.springframework.web.util
Class WebUtils


public abstract class WebUtils

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

Author:
Rod Johnson, Juergen Hoeller

Field Summary
 final static StringDEFAULT_WEB_APP_ROOT_KEY
          Default web app root key: "webapp.root"
 final static String[]SUBMIT_IMAGE_SUFFIXES
          Name suffixes in case of image buttons
 final static StringTEMP_DIR_CONTEXT_ATTRIBUTE
          Standard Servlet spec context attribute that specifies a temporary directory for the current web application, of type java.io.File
 final static StringWEB_APP_ROOT_KEY_PARAM
          Web app root key parameter at the servlet context level (i.e.

Method Summary
 static CookiegetCookie(HttpServletRequest request, String name)
          Retrieve the first cookie with the given name.
 static ObjectgetOrCreateSessionAttribute(HttpServletRequest request, String name, Class clazz)
          Get the specified session attribute, creating and setting a new attribute if no existing found.
 static MapgetParametersStartingWith(ServletRequest request, String base)
          Convenience method to return a map from un-prefixed property names to values.
 static ObjectgetSessionAttribute(HttpServletRequest request, String name)
          Check the given request for a session attribute of the given name.
 static FilegetTempDir(ServletContext servletContext)
          Return the temporary directory for the current web application, as provided by the servlet container.
 static booleanhasSubmitParameter(ServletRequest request, String name)
          Checks 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 voidsetSessionAttribute(HttpServletRequest request, String name, Object value)
          Set the session attribute with the given name to the given value.
 static voidsetWebAppRootSystemProperty(ServletContext servletContext)
          Set a system property to the web application root directory.

Field Detail

DEFAULT_WEB_APP_ROOT_KEY

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

SUBMIT_IMAGE_SUFFIXES

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

TEMP_DIR_CONTEXT_ATTRIBUTE

public final 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

WEB_APP_ROOT_KEY_PARAM

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

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

getOrCreateSessionAttribute

public static Object getOrCreateSessionAttribute(HttpServletRequest request, String name, Class clazz)
throws ServletException
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:
request - current HTTP request
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:
ServletException - if the session attribute could not be instantiated

getParametersStartingWith

public static Map getParametersStartingWith(ServletRequest request, 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. Maps single values to String and multiple values to String array.
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: map containing request parameters without the prefix, containing either a String or a String[] as values

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

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

hasSubmitParameter

public static boolean hasSubmitParameter(ServletRequest request, String name)
Checks 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

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 on remove if none has existed before!
Parameters:
request - current HTTP request
name - the name of the session attribute

setWebAppRootSystemProperty

public static void setWebAppRootSystemProperty(ServletContext servletContext)
throws java.lang.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 toolkits 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

Association Links

to Class java.lang.String

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

to Class java.lang.String

Web app root key parameter at the servlet context level (i.e. web.xml): "webAppRootKey".

to Class java.lang.String

Default web app root key: "webapp.root"

to Class java.lang.String

Name suffixes in case of image buttons