目录
题目:
编写一个实现页面计数的计数器,要求当刷新页面时,不增加计数。 (提示:使用application和session内置对象)
分析:
网站的来访人数可能会很多,有可能超过20位整数,大整数类BigInteger
使用application范围的属性保存计数。
用户每次在第一次访问时才需要进行计数的操作,而重复刷新页面时则不应重复计数,使用isNew()判断用户是否是第一次访问。
Web开发属于多线程操作,所以在进行更改、保存时需要进行同步操作。
代码1:
<%@page import="java.math.BigInteger"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% BigInteger counter = (BigInteger)application.getAttribute("count");//当没有count属性的时候,返回的是null if(counter == null){ counter = new BigInteger("0"); } if(session.isNew()){//判断是否是新的用户(刷新的时候session不会变,所以不进行累加) synchronized (this){ counter = counter.add(new BigInteger("1"));//大整数进行加1 application.setAttribute("count", counter);//application修饰全局 } } %> <font color="red" size=6>您是第<%=counter %>个访问该网站的用户</font> </body> </html>
代码2:
下面再来看一个复杂的实现方式:(同样效果)
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ page import="java.io.*"%> <%--由于要使用IO操作,必须导入java.io包--%> <%@ page import="java.util.*"%> <%--Scanner在java.util包中定义--%> <%@ page import="java.math.*"%> <%--BigInteger定义在java.math包中--%> <!DOCTYPE html> <html> <head> <!-- <meta charset="UTF-8"> --> <title>Insert title here</title> </head> <body> <%! BigInteger count=null; //定义成全局变量 %> <%! public BigInteger load(File file){ //读取计数文件 BigInteger count=null; //接受计数文件 try{ if(file.exists()){ //如果文件存在,则读取 Scanner scan=null; //定义Scanner对象 scan=new Scanner(new FileInputStream(file));//从文件中读取 if(scan.hasNext()){ //存在内容 count=new BigInteger(scan.next());//将内容放到BigInteger类中 } scan.close(); //关闭输入流 }else{ //文件不惨在则创建新的 count=new BigInteger("0"); //第一次访问 save(file,count); //调用save(),保存新的文件 } }catch(Exception e){ e.printStackTrace(); } 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){ e.printStackTrace(); } } %> <% String fileName=this.getServletContext().getRealPath("/")+"count.txt"; //文件路径 File file =new File(fileName); if(session.isNew()){ synchronized(this){ //必须进行同步操作 count=load(file); count=count.add(new BigInteger("1"));//自增操作 save(file,count); //保存修改后的数据 } } %> <h2>您是第<%=count==null?0:count%>位访客!</h2> </body> </html>
结果:
蟹蟹支持,一起加油呀!!!