Java programming has been known, there is a garbage collector in Java mechanism, when it runs (usually automatically when system memory is low to a certain extent), the memory occupied by objects that are no longer in use is reclaimed, so in Java programs, we usually only consider creating objects and never caring about object cleanup. Finalize () is a special method that Java provides for a class. The garbage collector's work is roughly the same: once the garbage collector is ready to release the storage space occupied by unwanted objects, it first invokes the Finalize () method of those objects before it really reclaims the object's memory. By using Finalize (), you can perform some special work while the garbage collector is running. The following example illustrates a neat use of finalize ().
Nowadays, more and more Web forms are used in commercial application systems. In Web application, each page access is independent, not related to each other, even if multiple users at the same time access to the same page of the application, users are not aware of each other. What if you want to check which users are currently using the system, such as when you are ready to recover a data backup or a system upgrade that the system administrator would like to know about? The Web server based on Servlet and JSP technology provides the implied session and Application object, which can help the developer to keep and share some information. When a user accesses a Web application, the Web server automatically creates a Session object that allows the user to share data on all pages applied during the conversation period; application is a global object for Web applications. With session, application objects, you can achieve the purpose of tracking all user information.
When a user opens a browser to start requesting a Web application's login page, the Web service creates a session for that customer, and thereafter, during the timeout time of the session, the client uses this session (timeout time can be set, such as the Tomcat server is set in the Web.xml file for each application. If you use IE browser, the session corresponds to a connection identified by the client's IP address, the client process ID, and the same session with the same IP address, a window for the same process (such as a new window opened through the ie-file). So the session can be used to identify individual customer application connections.
Here is a sample
To facilitate processing, first build a simple class (user) to express user information and storage SessionID:
package com;
public class user {
public String name="";
public String sessionId="";
}
Another class (testsession) is used to process user login, logout, and other action information so that the system can track user information for the current connection.
package com;
import java.util.Vector;
import Com.user;
public class Testsession {
Public user use R
Private Vector Vsid;
Public testsession ()
{
User=new User ();
}
Public boolean verify (String username,string password)
throws Exception//authentication user/password
{
return true;
}
public void Setsessionvar (String sesid,vector sid) {
. User.sessionid=sesid;
This.vsid=sid;
}
private static synchronized void addappses (user puser,
Vector pvsid) {//Log a newly connected user
int pos=-1;
Use R L_user;
if (puser==null | | pvsid==null)
return:
for (int i=0;i<pvsid.size (); i++) {
l_user= (user) Pvsid.get (i) ;
if (l_user.sessionId.equals (Puser.sessionid)) {
pos=i;
break;
}
}
if (pos==-1) {
Pvsid.add (puser);
}
else{
Pvsid.set (pos,puser);
}
}
private static synchronized void removeappses (user puser,
Vector pvsid) {//removal of an exiting user
int pos=-1;
U Ser L_user;
if (puseR==null | | Pvsid==null)
return;
for (int i=0;i<pvsid.size (); i++) {
l_user= (user) pvsid.get (i);
if (l_ User.sessionId.equals (Puser.sessionid)) {
pos=i;
break;
}
}
if (pos!=-1) {
Pvsid.remove (POS);
}
}
protected void Finalize () {
This.removeappses (this. USER,THIS.VSID);
}
Public boolean login (String username) throws Exception
{//Process login
this. User.name=username;
This.addappses (this. USER,THIS.VSID);
return true;
}
Public Boolean logout () throws Exception
{//Processing logoff
this. Finalize ();
this. User=null;
This.vsid=null;
return true;
}
}