Everyone in the landing QQ, the computer landed on the QQ, if another machine also landed on the QQ account, then QQ account will be pushed down before.
We are now using the Web to do a very simple demo.
Let's start with a simple function,
Users have only one user, this entity is set to the account Hello, password world
This is done in order to simplify the database to check the user's account information.
First, let's take a look at our web.xml.
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns=
"Http://xmlns.jcp.org/xml/ns/javaee"
xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee Http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "
version=" 3.1 ">
<listener>
< listener-class>sessionlistener</listener-class>
</listener>
</web-app>
In Web.xml, a Httpsessionlistener is configured inside.
The implementation of class Sessionlistener is as follows
Import javax.servlet.http.HttpSession;
Import javax.servlet.http.HttpSessionEvent;
Import Javax.servlet.http.HttpSessionListener;
Import Java.util.HashMap;
/** * Created by Huluo on 2016/8/20. * * Public class Sessionlistener implements Httpsessionlistener {/** * This hashmap stores an account with user name-httpsession object A letter that can only be landed by one person
Interest.
* * public static hashmap<string,httpsession> Sessionmap = new hashmap<string,httpsession> (); @Override public void sessioncreated (Httpsessionevent httpsessionevent) {HttpSession session = Httpsessio
Nevent.getsession (); @Override public void sessiondestroyed (Httpsessionevent httpsessionevent) {HttpSession session =
Httpsessionevent.getsession ();
Delsession (session);
public static synchronized void Delsession (HttpSession sessions) {if (session!= null) {
Deletes a variable of record in single sign-on if (Session.getattribute ("users")!= null) { User user = (user) Session.getattribute ("Users");
SessionListener.sessionMap.remove (User.getusername ());
}
}
}
}
public static hashmap<string,httpsession> Sessionmap = new hashmap<string,httpsession> ();
This hashmap is used to store user login information, and an account can only be used by one person at a time.
Then is the first demo of the page, which is very simple, only a hyperlink, let it access to
${pageContext.request.contextPath}/login
Here the author is also simplified can not be simplified, directly linked to the account name password also do not have to fill. Direct landing on ...
Let's see Loginservlet.java------------------------------> Determine if this account is being logged by someone else. Logical judgment
The code is as follows
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import java.io.IOException;
/** * Created by Huluo on 2016/8/20. */@WebServlet (urlpatterns = "/login") public class Loginservlet extends HttpServlet {@Override protected void do Get (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {User user = new U
Ser ();
HttpSession session = Req.getsession ();
/** * This account has been landed * * * if (null!= SessionListener.sessionMap.get (User.getusername ())) { /** * will have logged the information removed, the new user login information into the * * * Forcelogoututils.forceuserlogout (User.get
Username ());
SessionListener.sessionMap.put (User.getusername (), session); }/** * This account is notBe landed/else {SessionListener.sessionMap.put (User.getusername (), session);
} session.setattribute ("Users", user);
Req.getrequestdispatcher ("result.jsp"). Forward (REQ,RESP);
}
}
And then we'll talk about Forcelogoututils.java.
This class is used, when the discovery of the account has been logged, the person who has landed on the session from the Sessionlistener.java in the HashMap to get, and remove the record in this HashMap
And the session invalidate off.
Import javax.servlet.http.HttpSession;
Import java.util.Enumeration;
/**
* Created by Huluo on 2016/8/20.
*/Public
class Forcelogoututils
{public
static void Forceuserlogout (String username)
{
if ( SessionListener.sessionMap.get (username)!= null)
{
HttpSession session = SessionListener.sessionMap.get ( username);
SessionListener.sessionMap.remove (username);
Enumeration E = Session.getattributenames ();
while (E.hasmoreelements ())
{
String sessionname = (string) e.nextelement ();
Session.removeattribute (sessionname);
}
Session.invalidate ();}}
After the results of the Loginservlet.java process come out, jump to a result.jsp this page
Page content is also only a hyperlink. Check if you've been kicked down
<%-- The following hyperlinks are used to verify that the landing is kicked down and kicked off the return to index.jsp page --%>
<a href= "${ PageContext.request.contextPath}/check "> Inspection </a>
<%--
Created by the IntelliJ idea.
User:administrator
date:2016/8/20 time:11:24 To change this
template use
File | Settings | File Templates.
--%>
contenttype= "Text/html;charset=utf-8" language= "java"%>
iselignored= "False"%>
If you are kicked back to the index.jsp page, if you have not been kicked, continue to stay in result.jsp
Whether to be kicked down
Finally, let's look at the logic in Checkservlet.java.
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import java.io.IOException;
/**
* Created by Huluo on 2016/8/20.
*
/@WebServlet (urlpatterns = "/check") public
class Checkservlet extends HttpServlet
{
@Override
protected void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException
{
User user = (user) req.getsession (). getattribute ("users");
if (null = = user)
{
resp.sendredirect ("index.jsp");
}
else
{
resp.sendredirect ("result.jsp");
}
}
}
Judging from the session, whether there is a users information, if there is continued to stay on the result.jsp page, if there is no redirect to index.jsp