Import java. io .*;
Import java. util .*;
Import com.sun.image.codec.jpeg .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. awt .*;
Import java. awt. image .*;
Public class extends servlet extends HttpServlet {
Private String strFile = "c: count.txt ";
Private Font mFont = new Font ("Courier", Font. BOLD, 12 );;
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String s;
Int intCount;
S = getCount ();
IntCount = new Integer (s). intValue ();
IntCount = intCount + 1;
WriteCount (intCount + "");
Response. setContentType ("image/jpeg ");
ServletOutputStream out = response. getOutputStream ();
BufferedImage image = new BufferedImage (110,16, BufferedImage. TYPE_INT_RGB );
Graphics g = image. getGraphics ();
G. setColor (Color. black );
G. fillRect );
G. setColor (Color. white );
G. setFont (mFont );
Char c;
For (int I = 0; I <10; I ++ ){
C = s. charAt (I );
G. drawString (c + "", I * 11 + 1, 12 );
G. drawLine (I + 1) * 11-, (I + 1) * 11 );
}
Required imageencoder encoder = required codec. createJPEGEncoder (out );
Encoder. encode (image );
Out. close ();
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
Public synchronized void writeCount (String content ){
Try {
PrintWriter pw = new PrintWriter (new FileOutputStream (strFile ));
Pw. println (content );
Pw. close ();
} Catch (IOException e ){
System. out. println ("Write file error:" + e. getMessage ());
}
}
Public synchronized String getCount (){
String strCount;
Int len;
Int leftLen;
Try {
BufferedReader inf = new BufferedReader (new FileReader (strFile ));
StrCount = inf. readLine ();
Inf. close ();
} Catch (Exception e ){
System. out. println ("Get counter error:" + e. getMessage ());
Return reChar ('0', 10 );
}
Len = strCount. length ();
If (len <= 0 ){
Return reChar ('0', 10 );
}
If (len> 10 ){
Return reChar ('9', 10 );
}
LeftLen = 10-len;
Return reChar ('0', leftLen) + strCount;
}
Private String reChar (char c, int num ){
Int I;
If (num <= 0) return "";
String s = "";
For (I = 0; I <num; I ++ ){
S = s + c;
}
Return s;
}
}