以下是CountServlet.java:
1 import java.awt.Color;
2 import java.awt.Font;
3 import java.awt.Graphics;
4 import java.awt.image.BufferedImage;
5 import java.io.ByteArrayOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.util.Properties;
10
11 import javax.imageio.ImageIO;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletOutputStream;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17
18 public class CountServlet extends HttpServlet {
19
20 private static final long serialVersionUID = 1L;
21
22 public void doGet(HttpServletRequest request, HttpServletResponse response)
23 throws ServletException, IOException {
24 response.setContentType("image/jpeg");
25 // 设置浏览器不要缓存此图片
26 response.setHeader("Pragma", "No-cache");
27 response.setHeader("Cache-Control", "no-cache");
28 response.setDateHeader("Expires", 0);
29
30 ServletOutputStream sos = response.getOutputStream();
31
32 BufferedImage image = new BufferedImage(80, 20,
33 BufferedImage.TYPE_INT_RGB);
34 Graphics g = image.getGraphics();
35
36 g.setColor(Color.WHITE);
37 g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
38 String value = getAccessCount(request);
39 int len = value.length();
40 // 最大访问次数不超过9999999
41 if (len > 7) {
42 value = "9999999";
43 } else {
44 String tmp = "";
45 for (int i = 0; i < 7 - len; i++) {
46 tmp = tmp + "0";
47 }
48 value = tmp + value;
49 }
50 g.drawString(value, 0, 18);
51 // 结束图像的绘制过程,完成图像
52 g.dispose();
53 // 避免因图像大小超过Servlet引擎输出缓冲区的大小而自动使用chunked编码
54 ByteArrayOutputStream bos = new ByteArrayOutputStream();
55 ImageIO.write(image, "JPEG", bos);
56 byte[] buf = bos.toByteArray();
57 response.setContentLength(buf.length);
58 bos.close();
59 sos.write(buf);
60 sos.close();
61 }
62
63 private String getAccessCount(HttpServletRequest request) {
64 // 得到引用页面的URL地址,并以此作为访问次数的检索关键字
65 String pageKey = request.getHeader("referer");
66 if (pageKey == null) {
67 return "0";
68 }
69
70 Properties settings = new Properties();
71 String countFilePath = getServletContext().getRealPath("/count.txt");
72 try {
73 FileInputStream fi = new FileInputStream(countFilePath);
74 settings.load(fi);
75 fi.close();
76 } catch (Exception e) {
77 e.printStackTrace();
78 }
79 String count = "0";
80 try {
81 count = settings.getProperty(pageKey);
82 if (count == null) {
83 count = "0";
84 }
85 int c = Integer.parseInt(count) + 1;
86 count = new Integer(c).toString();
87 settings.put(pageKey, count);
88 FileOutputStream fi = new FileOutputStream(countFilePath);
89 settings.store(fi, "the page is accessed:");
90 fi.close();
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94 return count;
95 }
96 }
以下是Count.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
本网页已被访问了<img src="/servlet/CountServlet">次
</body>
</html>