Copy codeThe Code is as follows:
// Filter class
Public class EcondingFilter implements Filter {
Private String charset = null;
Private ServletContext context = null;
Private String path = "";
/**
* Store data in a local file before destruction
*/
Public void destroy (){
// Obtain the attribute value in 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 destroy ");
}
Copy codeThe Code is as follows:
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System. out. println ("before doFilter ");
String path = (HttpServletRequest) request). getServletPath (); // obtain the relative path of each accessed action
// determine the path, if the login action is used, add 1 to the attribute in the saved context.
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 ("after doFilter ");
}
Copy codeThe Code is as follows:
Public void init (FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System. out. println ("filter initialization ");
// Obtain the encoding format
Charset = filterConfig. getInitParameter ("encoding ");
// Obtain the servletContext
Context = filterConfig. getServletContext ();
System. out. println (charset );
Path = context. getRealPath ("");
File file = new File ("D: \ text.txt ");
If (! File. exists () {// determine whether the file exists
// If the file does not exist, create a file and save it to disk D.
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 created file every time tomcat starts the service
Path = "d :\\ text.txt ";
// Read the number of objects accessed locally
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 the servletContext
Context. setAttribute ("nums", nums );
}
}
It is convenient to use a filter. It does not need to be manually called every time. It is automatically referenced when the web service is started. First of all, I wrote the init method based on the fact that every time the web service starts, the init method is called, and when the service is closed, the destory method is called to count the data file, this method is written to the init method and destory method, which can reduce the number of times the server is constantly read and written to the file. Each time we log on, we add 1 to the attr in the servletContext, in this way, when the service is disabled, the files are stored in the disk. Read from the disk next time.