Hello everyone, this is my first blog. Sorry for the poor writing.
I have been asking questions about the number of online users and the number of online users today. I have found many online users, but they are not very good. They have many advantages and are not satisfactory. So I just made it myself, now I post the code for you to see ,,....
First: JSP ---------------------------- index. jsp
I want to write a javascript code in index. jsp.
<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
Window. onbeforeunload = function () // Author: meizz
{
VaR n = Window. event. screenx-window. screenleft;
VaR B = n> document.doc umentelement. scrollWidth-20;
If (B & window. event. clienty <0 | window. event. altkey)
{
Location. href = "sessioninvalidate"; // sessioninvalidate is a servlet that handles session invalidation.
}
}
</SCRIPT>
</Head>
<Body>
<Form action = "loginaction" method = "Post">
<Input type = "text" name = "username">
<Input type = "password" name = "password">
<Input type = "sunmit">
</Form>
</Body>
</Html>
Loginaction --------------------------------------------------------------------
Package com. dkp. servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Import com. dkp. Bean. users;
Import com. dkp. Dao. usersdao;
Import com. dkp. Dao. impl. usersdaoimpl;
Public class loginaction extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Dopost (request, response );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String username = request. getparameter ("username"); // get Username
String Password = request. getparameter ("password"); // obtain the password
Usersdao = new usersdaoimpl (); // create an object. This is my authenticated user Dao and Its impl (Implementation)
Users user = usersdao. finduserbyusername (username); // return the user according to the user name
If (user! = NULL & User. GetPassword (). Equals (password) {// determines whether the returned user is blank and whether the user's password is consistent with the entered password
// If the user is not empty and the password is the same
Httpsession session = request. getsession ();
// Put the user into the session
Session. setattribute ("users", user );
Response. sendredirect ("index. jsp ");
Return;
} Else {
Response. sendredirect ("index. jsp ");
}
}
}
Sessioninvalidate -------------------------------------------
Package com. dkp. servlet;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Import com. dkp. Action. onlineuser;
Public class sessioninvalidate extends httpservlet {
/**
*
*/
Private Static final long serialversionuid =-2799996612094958606l;
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Httpsession session = request. getsession ();
Session. removeattribute ("users ");
Session. invalidate ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}
---------------------- Onlineuser ----------- this class is the most important onlineuser and is a session listener.
Package com. dkp. Action;
Import java. util. hashmap;
Import java. util. Map;
Import javax. servlet. http. httpsessionattributelistener;
Import javax. servlet. http. httpsessionbindingevent;
Import javax. servlet. http. httpsessionevent;
Import javax. servlet. http. httpsessionlistener;
Import com. dkp. Bean. users;
Public class onlineuser implements httpsessionlistener,
Httpsessionattributelistener {
Private Static int activesessions = 0;
Private Static Map <string, users> online = new hashmap <string, users> ();
Public static Map <string, users> getonline (){
Return online;
}
Public static int getactivesessions (){
Return activesessions;
}
Public void attributeadded (httpsessionbindingevent event ){
If (event. getname (). Equals ("users ")){
System. Out. println ("user logged in .");
Users users = (users) event. getsession (). getattribute ("user ");
Online. Put (event. getsession (). GETID (), users );
}
}
Public synchronized void attributeremoved (httpsessionbindingevent event ){
If (event. getname (). Equals ("users ")){
System. Out. println ("user logged out .");
Online. Remove (event. getsession (). GETID ());
}
}
Public void attributereplaced (httpsessionbindingevent event ){
}
Public void sessioncreated (httpsessionevent event ){
Activesessions ++;
System. Out. println ("Someone is here ");
}
Public void sessiondestroyed (httpsessionevent event ){
If (activesessions> 0 ){
Activesessions --;
System. Out. println ("Someone has gone ");
}
}
}
JSP + servlet + Java enables statistics of online users and online users
Dev.firnow.com time: Author: anonymous name Edit: mr. Abu CLICK: 855
[Comment]
------------------------------------------- The following is Web. xml.
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Listener>
<Listener-class> com. dkp. Action. onlineuser </listener-class>
</Listener>
<Servlet>
<Description> exit </description>
<Display-Name> exit </display-Name>
<Servlet-Name> sessioninvalidate </servlet-Name>
<Servlet-class> com. dkp. servlet. sessioninvalidate </servlet-class>
</Servlet>
<Servlet>
<Description> logon </description>
<Display-Name> logon </display-Name>
<Servlet-Name> loginaction </servlet-Name>
<Servlet-class> com. dkp. servlet. loginaction </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> sessioninvalidate </servlet-Name>
<URL-pattern>/sessioninvalidate </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> loginaction </servlet-Name>
<URL-pattern>/loginaction </url-pattern>
</Servlet-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
<Welcome-File> admin/index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>