荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: huanghm (东方不败), 信区: Java
标 题: 访问计数器---使用JAVA Applet和CGI afei (转寄)
发信站: 深大荔园晨风站 (Wed Jan 21 10:15:51 1998), 转信
发信人: dong (春天来了...), 信区: Java
标 题: 访问计数器---使用JAVA Applet和CGI
发信站: BBS 水木清华站 (Fri Apr 11 13:58:40 1997)
这里有个JAVA和CGI程序结合的访问计数器的例子.
其实单纯做访问计数器,只有CGI就够了.
但希望统计用户访问该主页的时间时,只用CGI好象就不行了. (本例没有这个功能. :PPP)
count.pl
--------------------------------------------------------------
#!/usr/local/bin/perl
# Increments a visit count stored in the file named
# "counter" and send the count as a plain text document.
# Print a minimal MIME header
print "Content-type: text/plain\n\n";
$counterfile = "counter";
# Open the counter file and get current count
open(COUNTER, "$counterfile");
# Lock the file to guard against another process updating the
# file as this script uses it
$lock_exclusive = 2;
$unlock = 8;
flock(COUNTER, $lock_exclusive);
# Read and increment the count
$line = <COUNTER>;
close(COUNTER);
chop($line);
$count = $line;
$count++;
# Save the new count in the file
open(COUNTER, ">$counterfile");
print COUNTER ("$count\n");
close(COUNTER);
# Remember to unlock the file
flock(COUNTER, $unlock);
# Send count to caller
print "$count\n";
------------------------------------------------------------
VisitCount.java
----------------------------------------------------------
//---------------------------------------------------------------
// File: VisitCount.java
// Accesses a CGI program to update visit count and display
// the current count.
// Compile with: javac VisitCount.java
/*
* The VisitCount class is a Java applet that accesses a CGI
* program at the host URL and retrieves the visitor count (the
* CGI program also updates the count). Then it displays the
* current count. Use an <applet> tag to embed this applet
* at the location where you want to display the count.
*/
//---------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class VisitCount extends java.applet.Applet
{
String visitCount; // String to store visitor count
// Class initialization method
public void init()
{
try
{
// Open the URL that counts visitors to this site
// *** Edit URL before using at your site ***
URL url = new URL(
"http://www.lnbsoft.com/exec-bin/count.pl?update=1");
// Open a data stream and read a single line
try
{
DataInputStream in = new
DataInputStream(url.openStream());
visitCount = in.readLine();
}
catch(IOException e) {}
}
catch(MalformedURLException e) {}
}
// Method that paints the output
public void paint(java.awt.Graphics gc)
{
// Display the visitor count that was read from the URL
// Use whatever font style and color you want
// I used Helvetica for a message and a Courier font
// for the visitor count
Font helv = new Font("Helvetica", Font.BOLD, 16);
Font courier = new Font("Courier", Font.BOLD, 24);
gc.setFont(helv);
FontMetrics fm = gc.getFontMetrics();
String message = "Visitor number: ";
int xstart = 0;
int ystart = fm.getHeight();
gc.drawString(message, xstart, ystart);
// Advance horizontally by amount needed to display message
xstart += fm.stringWidth(message);
// Change font to Courier and display the count
gc.setFont(courier);
gc.setColor(Color.red);
gc.drawString(visitCount, xstart, ystart);
}
}
-----------------------------------------------------------------
HTML文件中调用方法.
<applet code=VisitCount width=200 height=26 align=middle></applet>
--
高山有崖,林木有枝,忧来无方,人莫之知。
m2m※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: dbsun20.cs.tsin]m
--
清 秀 绝 俗 ,容 色 照 人 ,身 形 婀 娜 ,
被裹在一袭宽大缁衣之中 , 仍掩不住窈窕娉婷姿态 。
( @ @ )
+----------------oOOo-(_)-oOOo------------------+
| 天涯何处没芳草......呜呼哀哉 ? |
+-----------------------------------------------+
※ 来源:.深大荔园晨风站 bbs.szu.edu.cn.[FROM: 202.192.140.254]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店