There are several methods for jsp to obtain the session value from the action:
For example, there is a session ("sessionid", "sessionValue") in my action ")
1. Use the struts tag to obtain: <s: property value = "# session. sessionid"/>
Ii. <% = request. getSession. getAttribute ("sessionid");> session is also one of the built-in objects and can be used directly, which is more convenient than request. getSession.
It can also be written as <% = session. getAttribute ("sessionid");>
3. Get el expressions: $ {sessionScope. sessionid}
The same is true if a bean is assigned, similar to $ {sessionScope. bean. beanProperty}
Generally, the session time is 30 minutes. If you determine whether a session exists
Copy codeThe Code is as follows:
<%
String id = "";
String username = "";
User user = (User) session. getValue ("docUser ");
// Determine whether the user is empty. If the user is not empty, perform the following operations. Otherwise, a null pointer exception is reported.
If (user! = Null ){
Id = user. getId ();
Username = user. getUsername ();
}
%>
You can also clear the session
Obtain the session and set its value to null.
Copy codeThe Code is as follows:
<%
Lduser lduser = (Lduser) ActionContext. getContext (). getSession (). get ("lduser ");
If (lduser = null ){
Session. putValue ("lduser", null );
Out. println ("session to close ");
%>