ServletContext Implement website counter

Source: Internet
Author: User
In web development, there are many features that need to be used in ServletContext, such as:
1. Website counter
2. Web site online user's display

3. Simple chat System


In summary, if it involves not sharing the data with the user, and the data is small, and you do not want to write to the database, we can consider using ServletContext to achieve effective clicks:
1. Once you have visited the webpage, even once, refresh is counted once, a bit fake
2. Different IP access, calculate once; if the same IP at a certain time (such as a day), no matter how many times to browse the Web page is counted once
3. The user launches the website, once again accesses the page calculates once


Implementation scenario:
1. When a user accesses the page, it is added one time in the database (for example, there is a table)
Disadvantage: Too frequent access to the database, too much pressure on the server
Advantages: Very good understanding, or to achieve
2. Set a static variable that can be initialized by a file or table when the server is started, and then write the value of the variable to a file or database when the server shuts down
Disadvantages: complex implementation, requiring the operation of the file
Advantages: High efficiency, not frequent access to files or databases
3. Using ServletContext, it is implemented in a similar way to the second


Implementing a Web Site counter
1. Operation of ServletContext in Wel.java, once per refresh once
2. In the Logincl.java operation ServletContext, the user each successful landing calculates once
3. The optimization of the 2 method (reduce the operation of the file) each user login, it is necessary to operate a file, so inefficient, can reduce the number of operations on the file:
In the life cycle of the servlet--
The initial value of the attribute times in ServletContext can be initialized in the init () method
In the Destroy () method, you can serveletcontext the property times in a one-time
The corresponding value is written to the file


Code implementation:
In the Logincl.java:
1. Rewrite the init () method

public void Init () throws Servletexception
{
	try{
		/* will only be invoked once
		/System.out.println ("Init is called");
		 * * ======== add page access to the function (read part) ===========
		 * User successfully login once to calculate a
		 /
		filereader	fr	= new FileReader ("D://mycounter.txt");
		BufferedReader	br	= new BufferedReader (FR);
		String		Numval	= Br.readline ();
		Br.close ();

		* * Put the Times value into the ServletContext
		/This.getservletcontext () setattribute ("Visittimes", numval);
		/* ======== The ability to add page access (read part) ===========/
	}catch (Exception ex) {
		ex.printstacktrace ();
	}
}
2. Add Count function in Process () method
 * * ======== Add page access number function (calculation part) =========
 * The value of Visittime in ServletContext + +/
 String times
= This.getservletcontext (). getattribute ("Visittimes"). toString ();
/* Value of times + + re-write back to ServletContext/
This.getservletcontext (). setattribute ("Visittimes", Integer.parseint ( Times) + 1));
/* ======== Add Page access Count function (calculate part) ========= * *
3. Rewrite the Destroy () method
public void Destroy ()
{
	try{
		System.out.println ("destroy called");
		 * * ======== Add page access number of features (write part) ===========
		 * And then write back the new number of times file	/FileWriter fw = new	FileWriter (" D://mycounter.txt ");
		BufferedWriter	BW	= new BufferedWriter (FW);
		Bw.write (This.getservletcontext (). getattribute ("Visittimes"). toString ());
		Bw.close (); /* Close file stream/////
		* ======== Add page access number (write part) =========== * *
	}catch (Exception ex) {
		ex.printstacktrace (); c18/>}
}
4. On the page display (Wel.java)
/* Display Web page access times
/pw.println ("This page has been accessed" + this.getservletcontext (). getattribute ("Visittimes"). ToString () + "secondary <br > ");
/* Display host IP address *
/PW.PRINTLN ("Your IP address =" + req.getremoteaddr () + "<br>");
/* Display host name
/PW.PRINTLN ("Your Host name =" + req.getremotehost () + "<br>");
Pw.println ("</center></body>");


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.