JSP's understanding of Request.getsession (false) (with a bug that programmers often neglect) _jsp programming

Source: Internet
Author: User
Tags assert sessions
"The words in front"
On the internet often see someone to request.getsession (false) questions, I was very confused for the first time, looked at the j2ee1.3 API, see how the official website explains.
"Official explanation"
GetSession
Public HttpSession getsession (Boolean Create)
Returns the current HttpSession associated with this request or, if if there are no current sessions and create is true, ret Urns a new session.
IF Create is false with the request has no valid HttpSession, this method returns NULL.
To make sure the sessions is properly maintained, and you are must call this method before the response is committed. If The container is a using cookie to maintain session integrity and are asked to create a new sessions when the response is Committed, an illegalstateexception is thrown.
Parameters:true-to create a new session for this request if necessary; False to return NULL if there ' s No.
Returns:the HttpSession associated with this request or null if create are false and the request has no valid session
Translated:
GetSession (Boolean create) means to return the httpsession in the current reqeust, if the httpsession in the current reqeust is null, and when Create is true, a new session is created , otherwise null is returned;
Nutshell:
Httpservletrequest.getsession (ture) is equivalent to Httpservletrequest.getsession ()
Httpservletrequest.getsession (False) is equivalent to if the current session is not NULL;
"Issues and Bugs":
Many of my colleagues around me wrote this;
Copy Code code as follows:

HttpSession session = Request.getsession (); A new session created if no session exists, haha! It's finished! If the session doesn't exist, you create another one!
String user_name = Session.getattribute ("user_name");

The point to note is that request.getsession () is equivalent to Request.getsession (true), unless we confirm that the session must exist or that Sesson does not exist, the need to create a session is clear. Otherwise try to use Request.getsession (false). When you use the Request.getsession () function, you usually check to see if a variable/tag is stored in the session in the action. There may be no session in this scenario, and the normal judgment should be this:
Copy Code code as follows:

HttpSession session = Request.getsession (false);
if (session!= null) {
String user_name = Session.getattribute ("user_name");
}

"Opportunistic":

If spring is used in the project (in fact, as long as it is a slightly larger project in Java, Spring is a good choice), it is much more convenient for the session to operate. If you need to take a value in session, you can use the Getsessionattribute of the Webutils tool (Org.springframework.web.util.WebUtils) (httpservletrequest Request, String name) method, look at the source of the master wrote: haha.
Copy Code code as follows:

/**
* Check the given request for a session attribute of the given name.
* Returns NULL if there is no sessions 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, or <code>null</code> if not found
*/
public static Object Getsessionattribute (HttpServletRequest request, String name) {
Assert.notnull (Request, "request must not to be null");
HttpSession session = Request.getsession (false);
Return (Session!= null Session.getattribute (name): null);
}

Note: Assert is a tool in the Spring Toolkit that is used to judge some validation operations, in this case, to determine if the reqeust is empty, and to throw an exception if it is empty.
The above code can be concise again, see:
Copy Code code as follows:

HttpSession session = Request.getsession (false);
String user_name = Webutils.getsessionattribute (reqeust, "user_name");

Source: http://blog.csdn.net/xxd851116
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.