ZhangLiHai.Com Blog


用servlet实现计数器

张利海 于 2003年07月03日 发表

ReadWriterFile.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
@定义操作文件方法
@Author : Alec Cheung
@MSN : lihai.zhang@polysoftcorp.com
@Home : http://cheung.cxc.cc
@Date : 2003-6-29
*/

public class ReadWriterFile extends HttpServlet
{

private static final String CONTENT_CHARSET = "plain/text;charset=GBK";

public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out = res.getWriter();
res.setContentType(CONTENT_CHARSET);
out.println("HTTP 1.0 METHOD GET.");
}

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doGet(req,res);
}

public void setLogFile(String file,String content)
{
try{
PrintWriter pw = new PrintWriter(new FileWriter(file,true),true);
pw.println(content);
pw.close();
}
catch(IOException e){
System.err.println("setLogFile : "+e);
}
}

public void setCountFile(String file,String content)
{
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.println(content);
pw.close();
}
catch(IOException e){
System.err.println("setCountFile : "+e);
}
}

public String getCountFile(String file)
{
String b = null;
try{
BufferedReader br = new BufferedReader(new FileReader(file));
b = br.readLine();
br.close();
return b.trim();
}
catch(IOException e){
//System.err.println("getCountFile : "+e);
return "0";
}
}

public void destroy()
{

}

}


Counter.java

import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.BufferedImage.*;
import java.awt.GraphicsEnvironment.*;
import com.sun.image.codec.jpeg.*;
import javax.swing.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
@写入数据并且生成图片
@Author : Alec Cheung
@MSN : lihai.zhang@polysoftcorp.com
@Home : http://cheung.cxc.cc
@Date : 2003-6-29
*/

public class Counter extends HttpServlet
{
ReadWriterFile rwf ;
Date date;
SimpleDateFormat sdf ;
DecimalFormat df ;
NumberFormatter nf ;
Graphics g ;

private static final String CONTENT_TYPE = "image/jpeg";
private String countFilePath = "C:\\Apache Group\\Apache\\htdocs\\count.txt";
private String logFilePath = "C:\\Apache Group\\Apache\\htdocs\\web.log";
private String screen ;
private String url ;
private String dt ;
private String ip ;
private String logContent ;
private String count;
private String isSession;
private int c;
private Integer ci;

public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
this.rwf = new ReadWriterFile();
this.sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
this.df = new DecimalFormat("000000");
this.nf = new NumberFormatter(df);
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{

date = new Date();
/*****Begin Write Log File*****/
screen = req.getParameter("screen");
url = req.getParameter("url");
dt = sdf.format(date);
ip = req.getRemoteAddr();
logContent = url+" "+screen+" "+dt+" "+ip;

rwf.setLogFile(logFilePath,logContent);
/***End Write Log File***/

try{
/****Begin Read Count File****/
count = rwf.getCountFile(countFilePath);
c = Integer.parseInt(count);
ci = Integer.valueOf(count);
count = nf.valueToString(ci); //over count;
/***End Read Count File***/
}
catch(Exception e){
System.err.println("NumberForatter : "+e);
}
HttpSession session = req.getSession(true);
isSession = (String)session.getAttribute("count.session");
if (isSession == null)
{
c++;
session.setAttribute("count.session","1");
rwf.setCountFile(countFilePath,String.valueOf(c));
}

try{

BufferedImage image = new BufferedImage(55,15,BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();
g.setColor( Color.white );
g.fillRect( 1,1,53,13);
g.setColor( Color.black );
g.fillRect( 2,2,51,11 );
g.drawImage(image,3,50,null);
g.setColor( Color.white );
g.drawString(count,6,12);
ServletOutputStream out = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();

}
catch(Exception e){

}

}

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doGet(req,res);
}

public void destroy()
{
//
}
}




counter.js

document.write("<img width=55 height=15 style=\\"cursor:hand\\" src=/servlet/Counter?"+
"url="+escape(window.location)+
"&screen="+escape(screen.width+"*"+screen.height)+" border=0 alt=\\"网站流量统计\\" >");



在页面里引用js文件就可以了。

新版本Blog中有更多内容
Copyright (C)2002-2007 All Rights Reserved Powered By:ZhangLiHai.Com