jsp Web page counter implementation example

  • 2020-11-20 06:12:23
  • OfStack

 
// The filter class  
public class EcondingFilter implements Filter { 
private String charset = null; 
private ServletContext context = null; 
private String path = ""; 
/** 
*  Store the data in a local file before destruction  
*/ 
public void destroy() { 
// To obtain servleContext The value of an attribute in  
String nums = (String) context.getAttribute("nums"); 
// Create write streams  
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 The destruction "); 
} 

 
public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException, ServletException { 
// TODO Auto-generated method stub 
System.out.println("doFilter before "); 
String path = ((HttpServletRequest)request).getServletPath();// Get for each access action Relative path of  
<img alt="" src="http://img.blog.csdn.net/20130728233435953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><img alt="" src="http://img.blog.csdn.net/20130728233445625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"> // Determine the path, if it's the one that logged in action Let's keep it context That property in there 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 after "); 

} 

 
public void init(FilterConfig filterConfig) throws ServletException { 
// TODO Auto-generated method stub 
System.out.println("filter Initialize the "); 
// Get encoding format  
charset = filterConfig.getInitParameter("encoding"); 
// To obtain 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 it 1 A file saved in D intraday  
file = new File("d:\\text.txt"); 
FileWriter fw = null; 
BufferedWriter bw = null; 
try { 
fw = new FileWriter(file); 
bw = new BufferedWriter(fw); 
bw.write(0 + "");//  Writes 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(); 

} 
} 
} 
// When every time tomcat When the service is started, the file that was created is read  
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 In the  
context.setAttribute("nums", nums); 
} 

} 

The convenience of using filters is that there is no need to invoke them manually every time, and the web service is automatically referenced when it is started. First of all, I wrote init method is based on, every time web service can call 1 init method, when close the service can call 1 destory method, will count the data files, this method writes init method and destory method, which reduces every constantly read the number of servers and read write file, when we landed every one, let the attr of servletContext plus 1, so as to realize when close service, put the files stored on disk. Next read from disk.

Related articles: