Create session learning-request. getSession ()

Source: Internet
Author: User

Reprinted from: http://hi.baidu.com/chentjj

In HttpServlet, The HttpSession object is usually created only when the request. getSession (true) method is called. The use of HttpSession is costly and requires the use of server resources. In line with the principle of being able to waste without wasting resources, I hope all sessions in the system will be under control, when we need to create a file, our code clearly creates the file. However, we recently found that new session objects often appear unexpectedly. Who is creating a session?

The most common error is that the request. getSession () function is used incorrectly. Generally, the action is used to check whether a variable or tag is stored in the session. In this scenario, no session may exist. The normal judgment should be as follows:

 

private boolean ifFlagExistInSession(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session != null) {
if (session.getAttribute("flagName") != null) {
return true;
}
}
return false;
}

 

The following statement may generate a new session that is not out of our intention:

private boolean ifFlagExistInSession(HttpServletRequest request) {
HttpSession session = request.getSession(); // a new session created if no session exists
if (session.getAttribute("flagName") != null) {
return true;
}
return false;
}


Note the request. getSession () is equivalent to request. getSession (true). Unless we confirm that the session exists or the sesson does not exist, we explicitly need to create the session. Otherwise, use the request whenever possible. getSession (false ).

FeedBack:
# Re: Who is creating session (1)-Inappropriate request. getSession ()
| Yeoying

In general (some servlet implementations may not be like this), the first time the user accesses the jsp page, the session will be created by default, because the jsp instruction session configuration is true, that is
<% @ Page session = "true" %>

The compiled java file contains code lines in the _ jspService () method (Tomcat is like this)

Session = pageContext. getSession ();

Unless you display the settings
<% @ Page session = "false" %>
To allow you to create a session when getSession (true) or getSession () is used.

In fact, creating a session does not consume any resources. It is nothing more than an empty map, that is, do not add too many things to it, especially in the cluster environment, it will increase the synchronization burden.

Success,

// Session usage in Struts Bean Method
String login_name = rs. getString ("true_name ");
// Return the request-related session
HttpSession session = request. getSession ();
// Save the login_name attribute value of truename in the session Object
Session. setAttribute ("truename", login_name );


False,

LoginFalse. loginFalse (request );

That is: I called the Russian static method in the LoginFalse class. Of course I wrote it myself. Recently I deliberately developed the habit of encapsulating everything. It feels good. The code in LoginFalse,

public static void loginFalse(HttpServletRequest request) {     String login_false = "Your username or password is wrong!!!";        HttpSession session = request.getSession();     session.setAttribute("loginfalse", login_false);}  

 

In the login. jsp code,

<% String loginwrong = (String) session. getAttribute ("loginfalse"); if (loginwrong! = Null) {%> <% = loginwrong %> <% // destroy session. removeAttribute ("loginfalse");} session. removeAttribute ("truename"); %>

 

<% @ Include file = "inc/logout. inc" %> is required for each page to display the user's true_name and logout functions.
Logout. inc code,

 

 <%String u = (String) session.getAttribute("truename");%>   
<%=u%><br/>
<%if (u == null) {%>
<logic:forward name="g_login"/>
<%}%>

 

 

 

The not required processing in LoginForm is omitted.
I feel that this problem has occurred. If I do not know the scope definition session in Struts, struts will give a getSession () by itself when I do not manually write the session in the Bean ();
There is a little different from the previous session in the JSP-JSP, in this:

 

HttpSession session = request. getSession ();

Before session. setAttribute, you must write this line.

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.