Reference article: 1. http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/
2.spring Source Code
Webutils
The webutils in the Org.springframework.web.util package is a very useful tool class that provides an easy-to-use proxy approach to many servlet APIs, reducing the complexity of accessing the Servlet API, which can be seen as a common Ser Vlet the façade class of the API method.
The following methods facilitate access to objects and properties in HttpServletRequest and HttpSession:
(1) Getsessionattribute
Gets the object that HttpSession a specific property name, otherwise you must do the same with Request.getSession.getAttribute (name);
Java code
- /**
- * 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 have existed before!
- * @param request current HTTP request
- * @param name The name of the session attribute
- * @return The value of the session attribute, or <code>null</code> if not found
- */
- Public static Object Getsessionattribute (httpservletrequest request, String name) {
- Assert.notnull (Request, "Request must not is null");
- HttpSession session = Request.getsession (false);
- return (Session! = null ? Session.getattribute (name): null);
- }
(2) Getrequiredsessionattribute
Similar to the previous method, except that it is mandatory to have the specified attribute in the HttpSession, otherwise throws an exception;
Java code
- /**
- * Check The given request for a session attribute of the given name.
- * Throws An exception if there are no session or if the session has no such
- * attribute. Does not create a new session if none have existed before!
- * @param request current HTTP request
- * @param name The name of the session attribute
- * @return The value of the session attribute, or <code>null</code> if not found
- * @throws IllegalStateException If the session attribute could not be found
- */
- Public static Object Getrequiredsessionattribute (httpservletrequest request, String name)
- throws IllegalStateException {
- Object attr = getsessionattribute (request, name);
- if (attr = = null) {
- Throw New illegalstateexception ("No session attribute " + name + "' Found" /c7>);
- }
- return attr;
- }
(3) Setsessionattribute
Sets the value for the specified attribute in the session, and removes the attribute from the session if the passed value is null
Java code
- /**
- * 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!
- * @param request current HTTP request
- * @param name The name of the session attribute
- * @param value The value of the session attribute
- */
- Public static void Setsessionattribute (httpservletrequest request, String name, Object value) {
- Assert.notnull (Request, "Request must not is null");
- if (value = null) {
- Request.getsession (). SetAttribute (name, value);
- }
- Else {
- HttpSession session = Request.getsession (false);
- if (session = null) {
- Session.removeattribute (name);
- }
- }
- }
(4) Exposerequestattributes
The MAP element is added to the ServletRequest attribute list, and when the request is directed (forward) to the next handler, the request properties can be accessed;
Java code
- /**
- * 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.
- * @param request current HTTP request
- * @param attributes the attributes Map
- */
- Public static void exposerequestattributes (servletrequest request, map< String,?> attributes) {
- Assert.notnull (Request, "Request must not is null");
- Assert.notnull (attributes, "attributes Map must not being null");
- for (map.entry<string,?> entry:attributes.entrySet ()) {
- Request.setattribute (Entry.getkey (), Entry.getvalue ());
- }
- }
(5) GetCookie
Gets the Cookie object for a specific name in the HttpServletRequest. If you need to create a Cookie, Spring also provides a handy cookiegenerator tool class;
Java code
- /**
- * Retrieve The first cookie with the given name. Note that multiple
- * Cookies can have the same name but different paths or domains.
- * @param request current servlet request
- * @param name Cookie name
- * @return The first cookie with the given name, or <code>null</code> if none is found
- */
- Public static Cookie GetCookie (httpservletrequest request, String name) {
- Assert.notnull (Request, "Request must not is null");
- Cookie cookies[] = request.getcookies ();
- if (cookie = null) {
- for (Cookie cookie:cookies) {
- if (Name.equals (Cookie.getname ())) {
- return cookies;
- }
- }
- }
- return null;
- }
(6) Extractfilenamefromurlpath
To intercept a file name from a URL address
Java code
- /**
- * Extract the URL filename from the given request URL path.
- * correctly resolves nested paths such as "/products/view.html" as well.
- * @param urlpath the request URL path (e.g. "/index.html")
- * @return The extracted URI filename (e.g. "index")
- */
- Public static string Extractfilenamefromurlpath (String urlpath) {
- String filename = Extractfullfilenamefromurlpath (URLPath);
- int Dotindex = filename.lastindexof ('. ' ) );
- if (Dotindex! =-1) {
- filename = filename.substring (0, dotindex);
- }
- return filename;
- }
- /**
- * Extract the full URL, filename (including file extension) from the given request URL path.
- * correctly resolves nested paths such as "/products/view.html" as well.
- * @param urlpath the request URL path (e.g. "/products/index.html")
- * @return The extracted URI filename (e.g. "index.html")
- */
- Public static string Extractfullfilenamefromurlpath (String urlpath) {
- int end = Urlpath.indexof ('; ') );
- if (end = =-1) {
- End = Urlpath.indexof ('? ' ) );
- if (end = =-1) {
- End = Urlpath.length ();
- }
- }
- int begin = Urlpath.lastindexof ('/', end) + 1 ;
- return urlpath.substring (begin, end);
- }
Spring's Webutils class source parsing