Copy code code as follows:
Filter class
public class Econdingfilter implements Filter {
Private String charset = null;
Private ServletContext context = null;
Private String Path = "";
/**
* Store data in local file before destruction
*/
public void Destroy () {
Gets the value of the property in the Servlecontext
String nums = (string) context.getattribute ("Nums");
Create a write stream
FileWriter FW = NULL;
BufferedWriter bw = NULL;
try {
FW = new FileWriter (path);
BW = new BufferedWriter (FW);
Bw.write (Nums);
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (BW!= null) {
Bw.close ();
}
if (fw!= null) {
Fw.close ();
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println ("filter Destruction");
}
Copy Code code as follows:
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
TODO auto-generated Method Stub
System.out.println ("Dofilter ago");
String Path = ((httpservletrequest) request). Getservletpath ();//Get the relative path of the action for each access
//Judgment Path, if the action is logged on, let the property in the saved context add 1
if (Path.endswith ("/login.action")) {
Context.setattribute ("Nums", Integer.parseint (Context.getattribute ("Nums"). ToString ()) +1+ "");
}
Request.setcharacterencoding (CharSet);
Response.setcharacterencoding (CharSet);
Chain.dofilter (request, response);
System.out.println ("Dofilter");
}
Copy Code code as follows:
public void init (Filterconfig filterconfig) throws Servletexception {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("filter Initialization");
Get encoding format
CharSet = Filterconfig.getinitparameter ("encoding");
Get ServletContext
context = Filterconfig.getservletcontext ();
System.out.println (CharSet);
Path = Context.getrealpath ("");
File File = new file ("D:\\text.txt");
if (!file.exists ()) {//Determine if the file exists
If the file does not exist, create a file and save it in the D disk
File = new file ("D:\\text.txt");
FileWriter FW = NULL;
BufferedWriter bw = NULL;
try {
FW = new FileWriter (file);
BW = new BufferedWriter (FW);
Bw.write (0 + "");/write initialization Data 0
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (BW!= null) {
Bw.close ();
}
if (fw!= null) {
Fw.close ();
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Read the file created when the service is started by Tomcat every time
Path = "D:\\text.txt";
A file that reads the number of people accessed from the local
FileReader FR = null;
BufferedReader BF = null;
String nums = "";
try {
FR = new FileReader (path);
BF = new BufferedReader (FR);
Nums = Bf.readline ();
System.out.println (Nums);
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (BF!= null) {
Bf.close ();
}
if (fr!= null) {
Fr.close ();
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Save the obtained data in ServletContext
Context.setattribute ("Nums", nums);
}
}
With the convenience of a filter, we do not need to manually call each time, when the Web service startup, automatically will be referenced. First of all, I wrote the Init method on the basis that every time a Web service startup invokes the Init method, the Destory method is invoked once the service is closed, and the data file that counts is written to the Init method and the Destory method. This can reduce every time the constant reading of the server and read the number of files written, when we log on every time, let the ServletContext in the attr plus 1, so that when the service is closed, the file saved in the disk. Read from disk the next time.