• 实现网站计数器


    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    <%@ page contentType="text/html; charset=GBK" %>
    <%@page import="java.math.BigInteger"%>
    <%@page import="java.io.File"%>
    <%@page import="java.io.FileInputStream"%>
    <%@page import="java.io.PrintStream"%>
    <%@page import="java.io.FileOutputStream"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>网站计数器</title>
    </head>

    <body>
    <%!
    BigInteger count
    = null;
    %>
    <%!
    public BigInteger load(File file){
    BigInteger count
    = null;
    try{
    if(file.exists()){
    Scanner scan
    = new Scanner(new FileInputStream(file));
    if(scan.hasNext()){
    count
    = new BigInteger(scan.next());
    }
    scan.close();
    }
    else{
    count
    = new BigInteger("0");
    save(file, count);
    }
    }catch (Exception e){
    System.out.println(e);
    }
    return count;
    }
    public void save(File file, BigInteger count){
    try{
    PrintStream ps
    = null;
    ps
    = new PrintStream(new FileOutputStream(file));
    ps.println(count);
    ps.close();
    }catch (Exception e){
    System.out.println(e);
    }
    }
    %>
    <%
    System.out.println(
    "count=========" + count);
    String fileName = this.getServletContext().getRealPath("/") + "count.txt";
    File file
    = new File(fileName);
    if(session.isNew()){
    synchronized(this){
    count
    = load(file);
    System.out.println(
    "count=========" + count);
    count
    = count.add(new BigInteger("1"));
    save(file, count);
    System.out.println(
    "count=========" + count);
    }
    }
    %>
    <h2>您是第<%=count == null ? 0 : count %>位访客</h2>
    </body>
    </html>

  • 相关阅读:
    收藏的 30 个CSS 代码段
    Power designer 15生成sql server 2005/2008列注释
    华为网盘
    敏捷宣言(一)
    敏捷宣言(三) Scrum是一种敏捷实践
    敏捷宣言(二) 何为敏捷
    我也曾哭过[转]
    你以为我穷,不好看,就没有感情吗?[转]
    关于异性朋友
    Gone With the Wind[转]
  • 原文地址:https://www.cnblogs.com/lihuiyy/p/2218116.html
Copyright © 2020-2023  润新知