Login from multiple users in the same browser on the same PC

Source: Internet
Author: User

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
  1. Public class order
  2. {
  3. Private string user;
  4. Private string password;
  5. Public String getuser ()
  6. {
  7. Return user;
  8. }
  9. Public void setuser (string user)
  10. {
  11. This. User = user;
  12. }
  13. Public String GetPassword ()
  14. {
  15. Return password;
  16. }
  17. Public void setpassword (string password)
  18. {
  19. This. Password = password;
  20. }
  21. }
  22. Public void dopost (httpservletrequest request, httpservletresponse response)
  23. Throws servletexception, ioexception
  24. {
  25. Order order = New Order ();
  26. Httpsession HS = request. getsession ();
  27. HS. invalidate ();
  28. String user = request. getparameter ("user ");
  29. String Password = request. getparameter ("password ");
  30. Order. setuser (User );
  31. Order. setpassword (password );
  32. HS. setattribute (user, order );
  33. Response. sendredirect ("wel. jsp ");
  34. }
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
  1. Public void dopost (httpservletrequest request, httpservletresponse response)
  2. Throws servletexception, ioexception
  3. {
  4. Order order = New Order ();
  5. Httpsession HS = request. getsession ();
  6. HS. invalidate ();
  7. String user = request. getparameter ("user ");
  8. String Password = request. getparameter ("password ");
  9. Order. setuser (User );
  10. Order. setpassword (password );
  11. HS. setattribute (user, order );
  12. Response. sendredirect ("wel. jsp? U = "+ User );
  13. }
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
  1. <%
  2. String u = request. getparameter ("U ");
  3. Httpsession HS = request. getsession ();
  4. Order order = (Order) HS. getattribute (U );
  5. Out. println ("user =" + order. getuser ());
  6. Out. println ("Password =" + order. GetPassword ());
  7. %>
<% 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 (); %>

 

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.