General description
A few days ago, I accidentally thought of this problem and felt it was quite practical and it was necessary to sort it out. It doesn't feel difficult to write a simple mode casually. It is actually quite simple to straighten out the idea.
In order to realize that users cannot log in at the same time, just think about Sina, Baidu, etc. in reality, just log in to one place and "squeeze" the other place, and you can know what the implementation result is. Then reverse it and you can form a clearer idea. Let’s discuss it together.
First of all, we must understand what login is used by users to log in, that is, the principle of users online. This is just storing the user's object in the session, then calling it in the frame, and other specific pages can also be directly referenced. Then the function of "squeezing down" is to make the newly generated session effective and make the session originally stored in the user invalid. At this point, the general idea has been established. So how to achieve it?
If you want to know how to implement it, you must understand the process of storing user objects in session. After the user logs in, we can get the user's object user, and the session.setAttribute(key,value); we store the user's userId or other unique identifier as key and save the user object as value. This way, you can call the only user anytime, anywhere. The problem of user storage has been solved, what about the problem of the abolishment of session when logging in?
This is actually not difficult. We can have more session characteristics, just like using map to store the user's identity as a key, and save the corresponding session as a value. Then when the user logs in repeatedly, you only need to take out the corresponding session and invalidate it.
At this point, the implementation idea has been clear. After so long, everyone is impatient to read the code, right? Here is the code:
Pre-preparation, jsp interface
The interface is simple, just a simple login interface
<form action ="<%=request.getContextPath()%>/UserWXPServlet" method = "post"> Username š<input type = "text" name = "username"/><br/> Password š<input type = "text" name = "password"/><br/> <input type = "submit" value ="submit"/> </form>
Jump to the page after success
Welcome: ${sessionScope.user.username}登陆!<br/>
I haven't written a failed page, you can write it yourself, and there is nothing to say about the failed page.
Implementation of entity and login
user's javabean
private String username; private String password; public User() { } public User(String user, String password) { super(); this.username = user; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }Log in to the user's service implementation method, so I won't write dao and interface here, everything is simple
public boolean domain(User user){ Properties pro = new Properties(); InputStream is = UserWXPServlet.class.getClassLoader().getResourceAsStream("user_wxp.properties"); String password = null; System.out.println(is+"--------->"+pro); if(user==null){ return false; } try { pro.load(is); password = pro.getProperty(user.getUsername()); if(user.getPassword()!=null&&user.getPassword().equals(password)){ System.out.println("Login successfully"); return true; } } catch (IOException e) { e.printStackTrace(); } finally{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return false; }Return true if login is successful, and false if it fails.
Servlet and corresponding logical tool classes
The next code is the real code for operating the user
I have defined two classes here, one tool class and one core servlet processing class
Some common elements are added to the tool class, such as the following code:
/** * Each user stores a session. Easy to operate in all kinds of ways! ! ! */ public static Map<String, HttpSession> mapSession = new HashMap<String,HttpSession>(); User exit code (must fly to abolish session or remove the corresponding user object): [java] view plain copypublic static void userLogout(String username){ if(mapSession.get(username)!=null){ //Get the session of the user who needs to exit HttpSession session = mapSession.get(username); //Remove the user in map<username,session>. Remember to exit the user, you must abolish the session or remove the user mapSession.remove(username); //Get the collection of attributes of the session Enumeration e = session.getAttributeNames(); //Delete all attributes while(e.hasMoreElements()){ String sessionName = (String) e.nextElement(); session.removeAttribute(sessionName); } //Repeal the session session.invalidate(); } }The Servlet code is as follows:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = new User(username, password); UserService userService = new UserService(); HttpSession session = request.getSession(); if(userService.dologin(user)){ //After login, insert the user into the session session.setAttribute("user", user); if(cheackSession(username)){ //If the session already exists before this, exit the user DbUtil.userLogout(username); } //Storage the new session in map<username,session> DbUtil.mapSession.put(username, session); //After the operation successful, jump, it is best to redirect here to let others know that the login has been successful request.getRequestDispatcher("login").forward(request, response); return ; } //Skip to the failure page here. If readers are interested, they can add it yourself}The code of checkSession(username) is as follows:
/** * Check whether this session is already contained * @param username * @return true: It already exists, it should be deleted! false: not present */ private boolean checkSession(String username){ HttpSession session = DbUtil.mapSession.get(username); if(session!=null){ return true; } return false; }Finally, attach the Servlet's xml configuration
<servlet> <description> For testing, users cannot log in repeatedly</description> <display-name>UserWXPServlet</display-name> <servlet-name>UserWXPServlet</servlet-name> <servlet-class>com.fingard.rabbit.wxp_test.Servlet.UserWXPServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserWXPServlet</servlet-name> <url-pattern>/UserWXPServlet</url-pattern> </servlet-mapping>
The above is the Java function that the editor introduced to you to implement the non-repeat login function of users. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!