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!