您现在的位置: 365建站网 > 365文章 > Java Bean实现网页来访计数器

Java Bean实现网页来访计数器

文章来源:365jz.com     点击数:354    更新时间:2009-10-12 09:40   参与评论

1.JSP页:


<%--
    Document   : index
    Created on : 2009-10-10, 14:47:46
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="count" class="MyBean.Counter"/>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor="#FFFFCC">
         <center>
              <font size="+5" face="楷体">Welcome!!</font><br>
              You're the
         <%
               String url = "C:\\Users\\lucifer\\Desktop\\count.txt";           //改成自己的text文件,用来计数
               count.setPath(url);
               count.setCount();
               out.println(count.getCount());
         %>
         Visitor!!!
         </center>
    </body>
</html>


2.Bean:

package MyBean;

import java.io.*;

/**
 *
 * @author lucifer
 */
public class Counter {
     private String url;
     private String count;

     public void setPath(String url){
          this.url = url;
     }

     public String getCount(){
          try{
               BufferedReader br;
               br = new BufferedReader(new FileReader(url));
               count = br.readLine();
               br.close();
               return count;
          }
          catch(IOException e){
               e.printStackTrace();
               return "No Record!";
          }
     }
     public void setCount(){
          try{
               BufferedReader br = new BufferedReader(new FileReader(url));
               count = new String(br.readLine());
               int num;
               if(count == null)
                    num = 1;
               else
                    num = Integer.parseInt(count) + 1;
               try{
                    PrintWriter pw = new PrintWriter(new FileOutputStream(url));
                    pw.println(num);
                    pw.close();
               }
               catch(IOException e){
                    System.out.println(e.getMessage());
               }
               br.close();
          }
          catch(IOException e){
               e.printStackTrace();
          }
     }
}

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (354人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号