当前位置: 技术问答>java相关
(java初学)如何使用java写一个页面计数器?
来源: 互联网 发布时间:2015-04-06
本文导语: 给个例子。 | 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...
给个例子。
|
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/plainnn";
$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 = ;
close(COUNTER);
chop($line);
$count = $line;
$count++;
# Save the new count in the file
open(COUNTER, ">$counterfile");
print COUNTER ("$countn");
close(COUNTER);
# Remember to unlock the file
flock(COUNTER, $unlock);
# Send count to caller
print "$countn";
------------------------------------------------------------
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 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.sina.com.cn/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文件中调用方法.
--------------------------------------------------------------
#!/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/plainnn";
$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 = ;
close(COUNTER);
chop($line);
$count = $line;
$count++;
# Save the new count in the file
open(COUNTER, ">$counterfile");
print COUNTER ("$countn");
close(COUNTER);
# Remember to unlock the file
flock(COUNTER, $unlock);
# Send count to caller
print "$countn";
------------------------------------------------------------
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 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.sina.com.cn/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文件中调用方法.