JSP Page Counter Implementation example

Source: Internet
Author: User
Tags stub

  Web Counter I guess everyone has seen it, record every visitor, below a good example, interested friends can refer to the following

  Code as follows://Filter class   public class Econdingfilter implements filter {  private String charset = null;  Priva Te ServletContext context = null;  private String Path = ""; /**  * Save data in local file before destroying   */  Public V OID Destroy () { //Get the value of the property in Servlecontext   String nums = (string) context.getattribute ("Nums"); // Create 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 {& nbsp   Try {  if (BW!= null) {  bw.close (); }  if (fw!= null) {  fw.close (); } } C Atch (IOException E) { /TODO auto-generated catch block  e.printstacktrace (); }   }  Sy Stem.out.println ("Filter Destroy"); }    code is 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 ()//Gets the relative path   <img alt=" "of the action for each access 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 ">//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 after ");   }     Code as follows: public void init (Filterconfig filterconfig) throws Servletexception { //TODO auto-generated Meth OD 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 file exists  //If file does not exist, create a file, save in D disk   file = new file ("D:text.txt");  FileWriter fw = null;  Buffe Redwriter 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) { /To Do auto-generated catch block  e.printstackTrace ();   } } } ///When the service is started by the Tomcat, the file   path = "D:text.txt"; // Files accessed from local read   FileReader FR = null;  BufferedReader bf = null;  String nums = "";  try {  FR = n EW FileReader (path);  bf = new BufferedReader (FR);  nums = Bf.readline ();  System.out.println (nums);   catch (Exception e) {  e.printstacktrace (); } finally {    try {  if (bf!= null) {&nbs P Bf.close (); }  if (fr!= null) {  fr.close (); } } catch (IOException e) { //TODO auto-g Enerated catch block  e.printstacktrace (); } } //Save the obtained data in ServletContext   Context.setattribute ("Nums", Nums); }   }    with a convenient point, does not require us to manually call each time, when the Web service starts, it is automatically 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. Next time from diskRead in.    
Related Article

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.