JSP to get the session from action, clear session and judge


There are several methods for JSP to obtain the session value from action

For example, I have a session (“sessionid”, “sessionvalue”) in my action

1、 Use the struts tag to get: < s: property value = “# session.sessionid / >

2、 <%= request.getSession.getAttribute (“sessionid”); > session is also one of the built-in objects request.getSession It’s much more convenient
It can also be written as <%= session.getAttribute (“sessionid”);>

3、 El expression gets:${ sessionScope.sessionid }

If a bean is assigned, the same is true, similar to${ sessionScope.bean.beanProperty }

Generally, the duration of a session is 30 minutes. If you determine whether a session exists or not

Copy codeThe code is as follows:
String id = “”;

String username = “”;

User user = (User)session.getValue(“docUser”);

//Judge whether the user is empty. If not, the following operations can be performed. Otherwise, a null pointer exception will be reported.
if(user != null){
id = user.getId();

username = user.getUsername();

You can also clear the session
Get 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) {
out.println (“session to close”);