Spring's Webutils class source parsing

Source: Internet
Author: User

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
  1. /**
  2. * Check The given request for a session attribute of the given name.
  3. * Returns NULL if there is no session or if the session has no such attribute.
  4. * Does not create a new session if none have existed before!
  5. * @param request current HTTP request
  6. * @param name The name of the session attribute
  7. * @return The value of the session attribute, or <code>null</code> if not found
  8.  */   
  9. Public static Object Getsessionattribute (httpservletrequest request, String name) {
  10. Assert.notnull (Request, "Request must not is null");
  11. HttpSession session = Request.getsession (false);
  12. return  (Session! = null ? Session.getattribute (name): null);
  13. }

(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
  1. /**
  2. * Check The given request for a session attribute of the given name.
  3. * Throws An exception if there are no session or if the session has no such
  4. * attribute. Does not create a new session if none have existed before!
  5. * @param request current HTTP request
  6. * @param name The name of the session attribute
  7. * @return The value of the session attribute, or <code>null</code> if not found
  8. * @throws IllegalStateException If the session attribute could not be found
  9.  */   
  10. Public static Object Getrequiredsessionattribute (httpservletrequest request, String name)
  11. throws IllegalStateException {
  12. Object attr = getsessionattribute (request, name);
  13. if (attr = = null) {
  14. Throw New illegalstateexception ("No session attribute " + name + "' Found"  /c7>);
  15. }
  16. return  attr;
  17. }

(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
  1. /**
  2. * Set The session attribute with the given name to the given value.
  3. * Removes the session attribute if value is null, if a session existed at all.
  4. * Does not create a new session if not necessary!
  5. * @param request current HTTP request
  6. * @param name The name of the session attribute
  7. * @param value The value of the session attribute
  8.  */   
  9. Public  static void Setsessionattribute (httpservletrequest request, String name, Object value) {
  10. Assert.notnull (Request, "Request must not is null");
  11. if (value = null) {
  12. Request.getsession (). SetAttribute (name, value);
  13. }
  14. Else  {  
  15. HttpSession session = Request.getsession (false);
  16. if (session = null) {
  17. Session.removeattribute (name);
  18. }
  19. }
  20. }

(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
  1. /**
  2. * Expose The given MAP as request attributes, using the keys as attribute names
  3. * and the values as corresponding attribute values. Keys need to be Strings.
  4. * @param request current HTTP request
  5. * @param attributes the attributes Map
  6.  */   
  7. Public static void exposerequestattributes (servletrequest request, map< String,?> attributes) {
  8. Assert.notnull (Request, "Request must not is null");
  9. Assert.notnull (attributes, "attributes Map must not being null");
  10. for (map.entry<string,?> entry:attributes.entrySet ()) {
  11. Request.setattribute (Entry.getkey (), Entry.getvalue ());
  12. }
  13. }

(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
  1. /**
  2. * Retrieve The first cookie with the given name. Note that multiple
  3. * Cookies can have the same name but different paths or domains.
  4. * @param request current servlet request
  5. * @param name Cookie name
  6. * @return The first cookie with the given name, or <code>null</code> if none is found
  7.  */   
  8. Public static Cookie GetCookie (httpservletrequest request, String name) {
  9. Assert.notnull (Request, "Request must not is null");
  10. Cookie cookies[] = request.getcookies ();
  11. if (cookie = null) {
  12. for (Cookie cookie:cookies) {
  13. if (Name.equals (Cookie.getname ())) {
  14. return  cookies;
  15. }
  16. }
  17. }
  18. return   null;
  19. }

(6) Extractfilenamefromurlpath

To intercept a file name from a URL address

Java code
  1. /**
  2. * Extract the URL filename from the given request URL path.
  3. * correctly resolves nested paths such as "/products/view.html" as well.
  4. * @param urlpath the request URL path (e.g. "/index.html")
  5. * @return The extracted URI filename (e.g. "index")
  6.  */   
  7. Public static string Extractfilenamefromurlpath (String urlpath) {
  8. String filename = Extractfullfilenamefromurlpath (URLPath);
  9. int Dotindex = filename.lastindexof ('. ' )  );
  10. if (Dotindex! =-1) {
  11. filename = filename.substring (0, dotindex);
  12. }
  13. return  filename;
  14. }
  15. /**
  16. * Extract the full URL, filename (including file extension) from the given request URL path.
  17. * correctly resolves nested paths such as "/products/view.html" as well.
  18. * @param urlpath the request URL path (e.g. "/products/index.html")
  19. * @return The extracted URI filename (e.g. "index.html")
  20.  */   
  21. Public static string Extractfullfilenamefromurlpath (String urlpath) {
  22. int end = Urlpath.indexof ('; ')  );
  23. if (end = =-1) {
  24. End = Urlpath.indexof ('? ' )  );
  25. if (end = =-1) {
  26. End = Urlpath.length ();
  27. }
  28. }
  29. int begin = Urlpath.lastindexof ('/', end) + 1  ;
  30. return  urlpath.substring (begin, end);
  31. }

Spring's Webutils class source parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.