zoukankan      html  css  js  c++  java
  • JSP---网页计数器(数据存在服务器的文本文件,防web应用程序关闭后数据丢失,防刷数据)

    1、建立进行读写文件的java类

    //写文件
    public static void WriteFile(String filename,long counter) throws IOException {

     PrintWriter out=new PrintWriter(new FileWriter(filename));
     out.println(counter);
     out.close();
    }


    //读文件
    public static long ReadFile(String filename) {
     long couter=0;
     File f=new File(filename);
     if(!f.exists()){
      try {
      WriteFile(filename, 0);
      } catch (IOException e) {
       e.printStackTrace();
     }
    }
     try {
      BufferedReader in=new BufferedReader(new FileReader(f));
     try {
      couter=Long.parseLong(in.readLine());
     } catch (NumberFormatException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     couter=0;
     }
    } catch (FileNotFoundException e) {
     e.printStackTrace();
    }
    return couter;
    }

    2.jsp页面调用

    <%
    String realPath = request.getSession().getServletContext()
    .getRealPath("/")
    + "counter.txt";
    long counter = Common.ReadFile(realPath);
    if(session.getAttribute("visitor")==null){
     session.setAttribute("visitor","yes");
     session.setMaxInactiveInterval(3600);
     counter++;
     Common.WriteFile(realPath, counter);
    }
    %>
    该页面已被访问<font color='red'><%=counter%></font>次

  • 相关阅读:
    代码操作
    购物车
    利息计算器
    生成海报
    知识库
    JavaScript处理字符串--参照W3C
    C#输入排序-冒泡
    enum举例
    C# 表达式计算器----数据结构
    C# 测试单词的完美度
  • 原文地址:https://www.cnblogs.com/beast-king/p/3851402.html
Copyright © 2011-2022 走看看