Simple picture counters can also be implemented in Java

Source: Internet
Author: User

Using Java to implement a simple picture of the counter, run diagram:

To learn more about Java, you can clickJava EE software engineer to learn.

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page language= "java" import= "java.io.*"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> counters </title>

<%!
Synchronizing Update counters

synchronized void counter () {

ServletContext Application=getservletcontext (); Constructing application objects (optional)

String Szpath=application.getrealpath ("/"); Get current path

szpath=szpath+ "Hits.txt"; Counter file 0-9999999999999 ...

String szrecord= ""; Count String

int nrecord=0; Count int

try{

BufferedReader file=new BufferedReader (New FileReader (szpath));

Szrecord=file.readline (); Read Counter file

}

catch (Exception e) {

System.out.println (e);

}

if (szrecord==null) {

szrecord= "0"; If 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 the file. This method of frequently writing files to save the count is undesirable.

Pw.close ();

}

catch (Exception e) {

System.out.println (e);

}

}

%>

<%

Display counter
if (Session.isnew ()) {//If it is a new session
Counter ();
}
String Path=application.getrealpath ("/");
Out.println (Path);
String szpath=path+ "Hits.txt";
String szrecord= "";
BufferedReader file=new BufferedReader (New FileReader (szpath));
try{
Szrecord=file.readline ();
if (szrecord==null) {
szrecord= "0";
}
}
catch (Exception e) {
System.out.println (e);
}
Displaying a 7-bit digital GIF image
String szout= "<body topmargin= ' 0 ' leftmargin= ' 0 ' > Current traffic:";
int i=0;
int K=7-szrecord.length (); Number of "0"
for (i=0;i<k;i++) {//display "0"
szout=szout+ "}
For (I=0;i<szrecord.length (); i++) {//Show non-"0"
szout=szout+ "}
szout=szout+ "</body>";
Out.println (szout);
%>

To learn more about the Java Learning program, you can focus on e mentor .

Counters for simple pictures are also available in Java

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.