What is ServletContext?
ServletContext is a common space on a server and is a data shared by different browsers.
The difference between a servletcontext and a cookie and a session can be seen from the graph.
What is the use of ServletContext?
1. Website counter
2. Display of Web users online
3. Simple Chat Tool
In short, if the design to different users to share with protective gear, while the amount of data and love is not small,
At the same time you do not want to write to the database, we can consider using ServletContext to implement.
Precautions:
Because the data in the ServletContext is stored in the server for a long time, it consumes memory, so we recommend not adding too much data to servletcontext ... Remember
How do I use Servletcontxt?
1. How to get ServletContext instances
This.getservletcontext ();
2. You can think of it as a table, which is very similar to the session:
Each row is an attribute.
Add attribute: SetAttribute (String name, Object ob);
Get value: GetAttribute (String name) returns an object
Delete attribute: RemoveAttribute (String name)
3. Life cycle
The life cycle of the attribute in ServletContext ends with the start of creation to the server shutdown.
How to implement a website counter?
How to count an effective click, each site has a different standard:
1. As long as you have visited the webpage, even if it is once, refresh it for you.
2. Different IP access to the page, count one click. If the same IP is within a certain amount of time, no matter how many times the browser accesses it.
3. The user visits the website, again visits also counts once.
implementation scenarios:
1. When a user accesses the webpage, it is added to the database one time.
Disadvantage: The database access is too frequent, the server pressure is too large.
Advantages: Very good understanding, the realization
2. Set a static variable, when the server starts, you can initialize the variable through a file or table, when the server shuts down, and then
The value of the variable is written to the file or to the database.
Cons: Complex to implement, requires file operation
Advantages: High efficiency, no frequent access to files or databases
3. Using ServletContext, its implementation is similar to the second.
The ability to add page access times
Create a FileReader
FileReader f=new FileReader ("E://mydocument.txt");
BufferedReader br=new BufferedReader (f);
Read a row of data
String Numval=br.readline ();
Be sure to close the file stream
Br.close ();
int Times=integer.parseint (numval);
Add one More Time
times++;
and write the new number back.
FileWriter w=new FileWriter ("E://mydocument.txt");
BufferedWriter bw=new BufferedWriter (w);
Bw.write (times+ "");
Bw.close ();
problem: Each user log in once, will operate a file, which has a verysafe but low efficiency, Could wereduce the number of operations on files?
The initial value of the property times in ServletContext can be initialized in the init () method.
In the Destroy () method, a one-time value that corresponds to the times of the properties in the ServletContext
Write to the file so that you can reduce the number of operations on the file.
<//overriding the Init function, Init will only be executed when the Tomcat call Servlet is started Public voidinit () {Try { //will only be called onceSystem.out.println ("Init is called"); //the ability to add page access times//Create a FileReaderFileReader f=NewFileReader ("E://mydocument.txt"); BufferedReader BR=NewBufferedReader (f); //read a row of dataString numval=Br.readline (); //Be sure to close the file streamBr.close (); inttimes=Integer.parseint (Numval); //put the Times value into ServletContext This. Getservletcontext (). SetAttribute ("Visittimes", times+ ""); } Catch(Exception ex) {}}//overriding the Destroy function, the Destroy function is called when Tomcat is closed, to use shutdown.bat, not to cross the Tomcat window directly Public voiddestroy () {Try{System.out.println ("Destroy is called"); //and write the new number back.FileWriter fw=NewFileWriter ("E://mydocument.txt"); BufferedWriter BW=NewBufferedWriter (FW); Bw.write ( This. Getservletcontext (). getattribute ("Visittimes"). toString ()); Bw.close (); } Catch(Exception ex) {}}
Add the above code on the login login page to perform a read file operation at each login and perform a write file operation once the Tomcat is closed.
ServletContext and website counters