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.