The JSP counter that will be used in many places, I hope to help everyone, do it as a collection. <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page language = "java" import = "java.io. *"%>
<%! // Synchronous Update Counter Synchronized Void Counter () {servletContext Application = GetServletContext (); // Constructs Application Object (Optional) String Szpath = Application.getRealPath ("/"); // Get Current Path Szpath = Szpath "hits.txt"; // counter file 0-99999999999999 ... String szrecord = ""; // count string int nRecord = 0; // Notes INT Try {BufferedReader File = New BufferedReader (New FileReader (Szpath) ); Szrecord = file.readline (); // read counter file} catch (exception e) {system.Println (e);} if (szrecord == null) {szrecord = "0"; // The counter file is empty} nRecord = java.lang.integer.Parseint (szrecord) 1; // counter 1 try {file f = new file (szpath); PrintWriter PW = New PrintWriter New FileWriter (f)); PW.Print (NRECORD); // Write files PW.Close ();} catch (exception e) {system.out.println (e);}}%>