日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

记录用户访问次数

發(fā)布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录用户访问次数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在Servlet中應(yīng)用ServletContext接口,Servlet容器在啟動一個Web應(yīng)用時,會為它創(chuàng)建一個ServletContext對象。當(dāng)Servlet容器終止一個Web應(yīng)用時,ServletContext對象也會被銷毀,所以該對象與Web應(yīng)用程序有同樣的生命周期

在ServletContext接口中存取共享數(shù)據(jù)的方法有:

  • setAttribute(String name,Object
    object):在ServletContext對象中存放共享數(shù)據(jù),參數(shù)name表示屬性名,參數(shù)object表示屬性值;

  • removeAttribute(String name):根據(jù)指定參數(shù)name屬性名,刪除ServletContext對象中的共享數(shù)據(jù);

  • getAttribute(String name):根據(jù)指定的參數(shù)name屬性,獲取ServletContext對象中的共享數(shù)據(jù);
    新建CounterServlet的Servlet類,在doPost()方法中實(shí)現(xiàn)統(tǒng)計用戶的訪問次數(shù)

    public class CounterServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//獲得ServletContext對象ServletContext context = getServletContext();//從ServletContext中獲得計數(shù)器對象Integer count = (Integer)context.getAttribute("counter");if(count==null){//如果為空,則在ServletContext中設(shè)置一個計數(shù)器的屬性count=1;context.setAttribute("counter", count);}else{ //如果不為空,則設(shè)置該計數(shù)器的屬性值加1context.setAttribute("counter", count+1);}response.setContentType("text/html"); //響應(yīng)正文的MIME類型response.setCharacterEncoding("UTF-8"); //響應(yīng)的編碼格式PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>統(tǒng)計網(wǎng)站訪問次數(shù)</TITLE></HEAD>");out.println(" <BODY>");out.print(" <h2><font color='gray'> ");out.print("您是第 "+context.getAttribute("counter")+" 位訪客!");out.println("</font></h2>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}}

新建CounterListener類

public class CounterListener implements ServletContextListener {/*** Web服務(wù)器終止時,調(diào)用該方法* 向保存計數(shù)器文件中寫一個當(dāng)前網(wǎng)站的訪問次數(shù)*/@Overridepublic void contextDestroyed(ServletContextEvent contextEvent) {ServletContext context = contextEvent.getServletContext();Integer count = (Integer)context.getAttribute("counter");if(count!=null){try{String path =context.getRealPath("/count.txt");File file = new File(path);FileWriter fw = new FileWriter(file);BufferedWriter bw = new BufferedWriter(fw);bw.write(context.getAttribute("counter").toString());bw.flush();bw.close();fw.close();}catch(Exception e){e.printStackTrace();}}}/*** 當(dāng)Web服務(wù)器啟動時,調(diào)用該方法* 讀取計數(shù)器文件中保存的網(wǎng)站訪問次數(shù)*/@Overridepublic void contextInitialized(ServletContextEvent contextEvent) {Integer count = 0;ServletContext context = contextEvent.getServletContext();try{String path =context.getRealPath("");File file = new File(path,"count.txt");System.out.println(file.getPath());if(!file.exists()){file.createNewFile();FileWriter fw = new FileWriter(file);BufferedWriter bw = new BufferedWriter(fw);bw.write("0");bw.flush();bw.close();fw.close();}else{FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);count = new Integer(br.readLine()); context.setAttribute("counter", count);br.close();fr.close();}}catch(Exception ex){ex.printStackTrace();}} }

web.xml文件配置

<servlet><servlet-name>CounterServlet</servlet-name><servlet-class>com.cn.zj.Servlet.CounterServlet</servlet-class></servlet><servlet-mapping><servlet-name>CounterServlet</servlet-name><url-pattern>/counter</url-pattern></servlet-mapping><listener><listener-class>com.cn.zj.Servlet.CounterListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>

總結(jié)

以上是生活随笔為你收集整理的记录用户访问次数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。