Session Purpose 1: Save User Information

Source: Internet
Author: User

A simple small system was designed using the MVC pattern. I don't know whether it is found. to display the logon user name, the user name must be transferred once every time the page is changed, which is very troublesome. Introducing the Session object will make the problem very simple.
First, let's take a look. After a user logs on to the system, the user name is his own. Then we can save the user information, because we just want to display the user name, so I use the Session object to save only the user name. How can this problem be solved?
After a user logs on to the system, the authentication is completed in the Controller LoginServlet. If the logon succeeds, the user name information is saved in the Session object. The Code is as follows:
// Put the user name into the session for later use
// Request. getSession (). setMaxInactiveInterval (3); Unit: seconds
Request. getSession (). setAttribute ("user", u );
The welcome. jsp page is used to receive the user name information of the session object. The Code is as follows:
String u = (String) session. getAttribute ("user ");
You also need to add some judgment. If the session storage time is reached (the user's daze time generally reaches 30 minutes, then the timeout, and the user name is blank), return to the login interface, otherwise, an unfriendly interface is displayed.
If (u = null ){
Response. sendRedirect ("login. jsp? Error = 1 ");
// Return;
} Else {
Out. println (u );
}
Return to the pre-Logon page, you can use the error parameter to determine whether the user is logged on normally. If not, some prompts are provided, prompting the user to log on abnormally and asking the user to log on.
String error = request. getParameter ("error ");
System. out. println ("error: ----------------" + error );
If (error! = Null ){
If (error. equals ("1 ")){
Out. println ("<font size = 6> you have not logged on properly. Please log on! </Font> ");
}
}
This is how Session stores user information. We hope the operation is successful!
 
 

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.