counter.jsp

来自「JSP操作文本文件进行网页访问量计数」· JSP 代码 · 共 40 行

JSP
40
字号
<%@   page   contentType="text/html;   charset=gb2312"%>   
<%@   page   import="java.io.*"%>   
<html>   
  <head>   
  <title>JSP网页记数器</title>   
  </head>   
  <body>   
  <%   
  BufferedReader   test; //BufferedReader对象,读取文件数据   
  String   tmp=null; //文本文件中存储的值   
  int   i=0; //记数值   
  String   path=request.getRealPath(""); //获取URL   
    
  File   f=new   File(path,"WEB-INF/count.txt"); //获取   File对象   在相应的位置建一个count.txt的文件
  test=new   BufferedReader(new   FileReader(f));   
  try{   
  tmp=test.readLine();   
  }catch(FileNotFoundException   e){   
  e.printStackTrace();   
  }   
  if(tmp==null){ //第一次运行时   
  i=0;   
  }   
  else{   
  i=(int)(Integer.parseInt(tmp)+1);   
  }   
  //*********************   
  try{   
  PrintWriter   outf=new   PrintWriter(new   FileOutputStream(f));   
  outf.println(i); //将i写入文件中   
  outf.close();   
  }   
  catch(FileNotFoundException   e)   {   
  e.printStackTrace();   
  }   
  test.close();   
  %>   
  <p   align="center">您是第<%=i%>位访客</p>   
  </body>   
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?