1. sessions are shared by the same browser.
But in the same browser and PC, their sessions are different ..
So it is not shared.
But if you are on the same machine and agree to the browser
You log on to two users .? Use session to save user objects.
The other one will overwrite the previous one.
For example
Java code
- Public class order
- {
- Private string user;
- Private string password;
- Public String getuser ()
- {
- Return user;
- }
- Public void setuser (string user)
- {
- This. User = user;
- }
- Public String GetPassword ()
- {
- Return password;
- }
- Public void setpassword (string password)
- {
- This. Password = password;
- }
- }
- Public void dopost (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception
- {
- Order order = New Order ();
- Httpsession HS = request. getsession ();
- HS. invalidate ();
- String user = request. getparameter ("user ");
- String Password = request. getparameter ("password ");
- Order. setuser (User );
- Order. setpassword (password );
- HS. setattribute (user, order );
- Response. sendredirect ("wel. jsp ");
- }
Public class order {private string user; private string password; Public String getuser () {return user;} public void setuser (string user) {This. user = user;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = PASSWORD ;}} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {order = New Order (); httpsession HS = request. getsession (); HS. invalidate (); string user = request. getparameter ("user"); string Password = request. getparameter ("password"); Order. setuser (User); Order. setpassword (password); HS. setattribute (user, order); response. sendredirect ("wel. JSP ");}
When you use session to save an object, the previous object will be washed out.
When you refresh the previous login page, you will program the information of the second login user.
So how can we agree to the browser and log on to two users on the same PC.
We can determine that different users are logged on based on the unique identifier or user name transfer method.
In this case, different values are stored in the session, and the unique user name is transferred through forwarding to get the corresponding object.
However, this performance is not good, because if there are thousands of customers logging on, isn't it necessary to save thousands of attribute values in the session?
So it is not advisable,
Very tangled Problem
The modified code is as follows:
Java code
- Public void dopost (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception
- {
- Order order = New Order ();
- Httpsession HS = request. getsession ();
- HS. invalidate ();
- String user = request. getparameter ("user ");
- String Password = request. getparameter ("password ");
- Order. setuser (User );
- Order. setpassword (password );
- HS. setattribute (user, order );
- Response. sendredirect ("wel. jsp? U = "+ User );
- }
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {order = New Order (); httpsession HS = request. getsession (); HS. invalidate (); string user = request. getparameter ("user"); string Password = request. getparameter ("password"); Order. setuser (User); Order. setpassword (password); HS. setattribute (user, order); response. sendredirect ("wel. JSP? U = "+ User );}
Then, when we use users
You can use the unique identifier (or user name) to obtain the corresponding object from the session.
Remember, every time you jump to a page, you need to pass it because you need to get the corresponding object from the session.
The Code is as follows:
Java code
- <%
- String u = request. getparameter ("U ");
- Httpsession HS = request. getsession ();
- Order order = (Order) HS. getattribute (U );
- Out. println ("user =" + order. getuser ());
- Out. println ("Password =" + order. GetPassword ());
- %>
<% String u = request. getparameter ("U"); httpsession HS = request. getsession (); Order order = (Order) HS. getattribute (U); out. println ("user =" + order. getuser (); out. println ("Password =" + order. getPassword (); %>