Spring WebUtils source code parsing

Source: Internet
Author: User

Spring WebUtils source code parsing

 

2. spring Source Code
WebUtils
At org. springframework. web. the WebUtils In the util package is a very useful tool class. It provides easy-to-use proxy methods for many Servlet APIs, reducing the complexity of accessing Servlet APIs, it can be seen as a door class of common Servlet API methods.
The following methods facilitate access to objects and attributes in HttpServletRequest and HttpSession:

 

(1) getSessionAttribute

Obtain the object of the specific attribute name of HttpSession. Otherwise, you must use request. getSession. getAttribute (name) to perform the same operation;

 

Java code
  1. /*** 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! * @ Param request current HTTP request * @ param name the name of the session attribute * @ return the value of the session attribute, ornullIf not found */public static Object getSessionAttribute (HttpServletRequest request, String name) {Assert. notNull (request, Request must not be null); HttpSession session = request. getSession (false); return (session! = Null? Session. getAttribute (name): null );}

    (2) getRequiredSessionAttribute

    Similar to the previous method, it only requires that the HttpSession has the specified attribute. Otherwise, an exception is thrown;

     

    Java code
    1. /*** 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! * @ Param request current HTTP request * @ param name the name of the session attribute * @ return the value of the session attribute, ornullIf not found * @ throws IllegalStateException if the session attribute cocould not be found */public static Object getRequiredSessionAttribute (HttpServletRequest request, String name) throws IllegalStateException {Object attr = getSessionAttribute (request, request, name); if (attr = null) {throw new IllegalStateException (No session attribute '+ name + 'found);} return attr ;}

      (3) setSessionAttribute

      Specifies the attribute setting value for the session. If the input value is null, this attribute is removed from the session.

       

      Java code
      1. /*** 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 be null); if (value! = Null) {request. getSession (). setAttribute (name, value);} else {HttpSession session = request. getSession (false); if (session! = Null) {session. removeAttribute (name );}}}

        (4) exposeRequestAttributes

        Add the Map element to the attribute list of ServletRequest. when the request is forwarded to the next handler, these request attributes can be accessed;

         

        Java code
        1. /*** 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 Attributes) {Assert. notNull (request, Request must not be null); Assert. notNull (attributes, Attributes Map must not be null); for (Map. Entry Entry: attributes. entrySet () {request. setAttribute (entry. getKey (), entry. getValue ());}}

          (5) getCookie

          Obtain the Cookie object of a specific name in HttpServletRequest. If you need to create a Cookie, Spring also provides a convenient CookieGenerator tool class;

           

          Java code
          1. /*** 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, ornullIf none is found */public static Cookie getCookie (HttpServletRequest request, String name) {Assert. notNull (request, Request must not be null); Cookie cookies [] = request. getCookies (); if (cookies! = Null) {for (Cookie cookie: cookies) {if (name. equals (cookie. getName () {return cookie ;}} return null ;}

            (6) extractFilenameFromUrlPath

            Extract the file name from the Url

             

            Java code
            1. /*** 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. lastInd ExOf ('.'); 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 extractFullFilen AmeFromUrlPath (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 );}

Related Article

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.