Servlet自学第29讲:使用ServletContext实现网站计数器

网站计数器的思考

分析网站计数器的相关业务逻辑:
这里写图片描述

网站计数器的相关实现:
建立一个recoder.txt外部文件,用于保存访问量,这样可以保证稳定增长。同时防止web应用或reload充值计数器的值

网站计数器的实现

建立initservlet 用于初始化servletContext 和 在关闭tomcat时destroy方法中保存访问量到recoder文件中:

配置文件中加入<load-on-startup>1</load-on-startup>

public class InitServlet extends HttpServlet {
    @Override
    public void destroy(){
        super.destroy();
        String path=this.getServletContext().getRealPath("/record.txt");
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fw = new FileWriter(path);
            bw = new BufferedWriter(fw);
            String nums = (String) this.getServletContext().getAttribute("nums");            
            bw.write(nums);            
        } catch (Exception ex) {
            Logger.getLogger(InitServlet.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                if(bw!=null){
                    bw.close();
                }
                if(fw!=null){
                    fw.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(InitServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
    }
    @Override
    public void init()throws ServletException{

        FileReader fr=null;
        BufferedReader br=null;
        try {
            String path=this.getServletContext().getRealPath("/record.txt");
            //打开文件
            fr = new FileReader(path);
            //转为BufferedReader 再读一行
            br = new BufferedReader(fr);
            String nums = br.readLine();
            //把nums放入ServletContext
            this.getServletContext().setAttribute("nums",nums);            
        } catch (Exception ex) {
            Logger.getLogger(InitServlet.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                if(br!=null){
                    br.close();
                }
                if(fr!=null){
                    fr.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(InitServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

当用户登陆一次时,我们取出访问量,并加1

//向servletContext中取出属性并添加
String num= (String)this.getServletContext().getAttribute("nums");
this.getServletContext().setAttribute("nums",(Integer.parseInt(num)+1)+"");

在页面显示

String num = (String)this.getServletContext().getAttribute("nums");
Out.println("该页面被访问了"+num+"次");

如果我们的tomcat异常退出怎么办?
在现实生活中,tomcat服务器异常退出(跟结束进程的操作类似)情况是比较少发生的,然而已发生会出现比较严重的后果,解决方法是使用线程,定时把ServletContext中的值写入recoder.txt 比如10min

最后要提醒的是,ServletContext是长期存在的,它的生命周期与servlet的init方法和destroy相关。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值