Jsp obtains and judges sessions and sessions sent from actions.

Source: Internet
Author: User

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 ");
%>

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.