00000002.htm
来自「水木清华BBS」· HTM 代码 · 共 149 行
HTM
149 行
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: dong (春天来了...), 信区: Java <BR>标 题: 访问计数器---使用JAVA Applet和CGI <BR>发信站: BBS 水木清华站 (Fri Apr 11 13:58:40 1997) <BR> <BR>这里有个JAVA和CGI程序结合的访问计数器的例子. <BR>其实单纯做访问计数器,只有CGI就够了. <BR>但希望统计用户访问该主页的时间时,只用CGI好象就不行了. (本例没有这个功能. :PPP) <BR> <BR>count.pl <BR>-------------------------------------------------------------- <BR>#!/usr/local/bin/perl <BR># Increments a visit count stored in the file named <BR># "counter" and send the count as a plain text document. <BR> <BR># Print a minimal MIME header <BR> print "Content-type: text/plain\n\n"; <BR> <BR> $counterfile = "counter"; <BR> <BR># Open the counter file and get current count <BR> open(COUNTER, "$counterfile"); <BR> <BR># Lock the file to guard against another process updating the <BR># file as this script uses it <BR> $lock_exclusive = 2; <BR> $unlock = 8; <BR> flock(COUNTER, $lock_exclusive); <BR> <BR># Read and increment the count <BR> $line = <COUNTER>; <BR> close(COUNTER); <BR> <BR> chop($line); <BR> $count = $line; <BR> $count++; <BR> <BR># Save the new count in the file <BR> open(COUNTER, ">$counterfile"); <BR> print COUNTER ("$count\n"); <BR> close(COUNTER); <BR> <BR># Remember to unlock the file <BR> flock(COUNTER, $unlock); <BR> <BR># Send count to caller <BR> print "$count\n"; <BR>------------------------------------------------------------ <BR>VisitCount.java <BR> <BR>---------------------------------------------------------- <BR>//--------------------------------------------------------------- <BR>// File: VisitCount.java <BR>// Accesses a CGI program to update visit count and display <BR>// the current count. <BR>// Compile with: javac VisitCount.java <BR>/* <BR> * The VisitCount class is a Java applet that accesses a CGI <BR> * program at the host URL and retrieves the visitor count (the <BR> * CGI program also updates the count). Then it displays the <BR> * current count. Use an <applet> tag to embed this applet <BR> * at the location where you want to display the count. <BR>*/ <BR>//--------------------------------------------------------------- <BR>import java.awt.*; <BR>import java.applet.*; <BR>import java.net.*; <BR>import java.io.*; <BR>public class VisitCount extends java.applet.Applet <BR>{ <BR> String visitCount; // String to store visitor count <BR>// Class initialization method <BR> public void init() <BR> { <BR> try <BR> { <BR>// Open the URL that counts visitors to this site <BR>// *** Edit URL before using at your site *** <BR> URL url = new URL( <BR> "<A HREF="http://www.lnbsoft.com/exec-bin/count.pl?update=1");">http://www.lnbsoft.com/exec-bin/count.pl?update=1");</A> <BR> <BR>// Open a data stream and read a single line <BR> try <BR> { <BR> DataInputStream in = new <BR> DataInputStream(url.openStream()); <BR> visitCount = in.readLine(); <BR> } <BR> catch(IOException e) {} <BR> } <BR> catch(MalformedURLException e) {} <BR> } <BR>// Method that paints the output <BR> public void paint(java.awt.Graphics gc) <BR> { <BR>// Display the visitor count that was read from the URL <BR>// Use whatever font style and color you want <BR>// I used Helvetica for a message and a Courier font <BR>// for the visitor count <BR> <BR> Font helv = new Font("Helvetica", Font.BOLD, 16); <BR> Font courier = new Font("Courier", Font.BOLD, 24); <BR> <BR> gc.setFont(helv); <BR> FontMetrics fm = gc.getFontMetrics(); <BR> String message = "Visitor number: "; <BR> int xstart = 0; <BR> int ystart = fm.getHeight(); <BR> gc.drawString(message, xstart, ystart); <BR> <BR>// Advance horizontally by amount needed to display message <BR> xstart += fm.stringWidth(message); <BR> <BR>// Change font to Courier and display the count <BR> gc.setFont(courier); <BR> gc.setColor(Color.red); <BR> gc.drawString(visitCount, xstart, ystart); <BR> } <BR>} <BR>----------------------------------------------------------------- <BR> <BR>HTML文件中调用方法. <BR><applet code=VisitCount width=200 height=26 align=middle></applet> <BR> <BR> <BR> <BR> <BR>-- <BR>高山有崖,林木有枝,忧来无方,人莫之知。 <BR> <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: dbsun20.cs.tsin] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?