zoukankan      html  css  js  c++  java
  • servlet-servletContext网站计数器

    1、在项目中新建文件夹新建文件nums.txt

    2、在web.xml文件配置

    <servlet>
         <description>This is the description of my J2EE component</description>
         <display-name>This is the display name of my J2EE component</display-name>
         <servlet-name>NumServlet</servlet-name>
         <servlet-class>servlet.NumServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
       </servlet>

    public class NumServlet extends HttpServlet {
        
         //复写init初始化方法,将数据读取到ServletContext对象中
         @Override
         public void init() throws ServletException {
             //获取文件路径
             String path=this.getServletContext().getRealPath("/nums/nums.txt");
             //声明流对象
             FileReader fr=null;
             BufferedReader br=null;
             try{
                 fr=new FileReader(path);
                 br=new BufferedReader(fr);
                 String nums=br.readLine();
                 System.out.println(nums);
                 this.getServletContext().setAttribute("nums",nums);
             }catch(Exception e){
                 e.printStackTrace();
             }finally{
                 try {
                     br.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 try {
                     fr.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
            
         }
        
        
         //复写销毁方法,存储计数器到文件中
         @Override
         public void destroy(){
             //获取网页计数器
             int nums=(int)this.getServletContext().getAttribute("nums");
             //获取文件路径
             String path=this.getServletContext().getRealPath("/nums/nums.txt");
             //声明流对象
             BufferedWriter bw=null;
             FileWriter fw=null;
             try{
                 fw=new FileWriter(path);
                 bw=new BufferedWriter(fw);
                 bw.write(nums+"");
                 bw.flush();
             }catch(Exception e){
                 e.printStackTrace();
             }finally{
                 try {
                     fw.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 try {
                     bw.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                
             }
         }
        
    }

    好好学习,天天向上。 努力工作,给自己的和家人一个满意的答案。。。。
  • 相关阅读:
    转载:SQL server2005 里面没有management studio!下载SQL开发版本
    LInux、Ubuntu、win7、win8纯净版 镜像包链接地址 收集
    [转]Lucene经验总结 (转注:较旧,但有干货)
    几篇调试IIS内存过高或CPU过高的好文章
    Web应用程序
    使用ASP.NET State Server实现多应用程序间共享Session State
    [转]使用visual studio进行web应用程序性能测试
    [转] ASP.NET WEB API程序在VS启动或发布到IIS后启动后发生
    [转]优化Redis内存的9个要点
    [转]使用Memcached的8个要点
  • 原文地址:https://www.cnblogs.com/axu521/p/9903748.html
Copyright © 2011-2022 走看看