国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

IDEA 當前在線人數和歷史訪問量的示例代碼

瀏覽:3日期:2024-08-21 09:10:44

當前在線人數

IDEA 當前在線人數和歷史訪問量的示例代碼

一共需要三處

創建監聽器

package com.count;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;/* 初始化: 只有服務器的啟動,才會創建servletContext對象。 用于監聽servletContext創建,一旦創建servletContext創建,則設置servletContext中的count值為0;*/@WebListener/* 這個注解的作用是啟動監聽,相當于在web.xml配置( <listener> <listener-class>com.cyl.count.InitServletContexListener</listener-class> </listener>*/public class InitServletContexListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { //獲取ServletContext域對象 ServletContext servletContext = servletContextEvent.getServletContext(); //給ServletContext域對象,設置count=0 servletContext.setAttribute('count',0); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { }}

package com.count; import javax.servlet.ServletContext;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener; /** * @監聽在線人數,監聽session的創建和銷毀 * 如果session創建 獲取ServletContext中的count++,重新設置 * 如果session銷毀 獲取ServletContext中的count--,重新設置 */@WebListenerpublic class OnlineNumberHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { //1.獲取session HttpSession session = httpSessionEvent.getSession(); ServletContext servletContext = session.getServletContext(); //2.獲取counnt值,加1 int count = (int) servletContext.getAttribute('count'); count++; //3.把servlet存儲到servletContext對象中 servletContext.setAttribute('count',count); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { //1.獲取session HttpSession session = httpSessionEvent.getSession(); ServletContext servletContext = session.getServletContext(); //2.獲取counnt值,減1 int count = (int) servletContext.getAttribute('count'); count++; //3.把servlet存儲到servletContext對象中 servletContext.setAttribute('count',count); }}

修改index.jsp

<%@ page contentType='text/html;charset=UTF-8' language='java' %><%@taglib prefix='c' uri='http://java.sun.com/jstl/core' %><html><head> <title>$Title$</title></head><body><h1>當前在線人數:${count}</h1></body></html>

歷史訪問量

IDEA 當前在線人數和歷史訪問量的示例代碼

import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class countServlet1 */@WebServlet('/countServlet1')public class countServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public countServlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設置字符編碼 request.setCharacterEncoding('utf-8'); response.setCharacterEncoding('utf-8'); response.setContentType('text/html; charset=utf-8'); //獲取全局的共享數據 ServletContext servletContext = this.getServletContext(); //獲取計數器count Integer count = (Integer) servletContext.getAttribute('count'); //如果獲取的計算器對象為空 ,說明是第一次訪問,并將count,放入servletCount if( servletContext.getAttribute('count') == null) { count = 1; servletContext.setAttribute('count', count); }else { //否則就不是第一次訪問,將登陸的計數器進行加1的數據更新 servletContext.setAttribute('count', count+1); } //將登陸的次數顯示在頁面上 PrintWriter out =response.getWriter(); out.print('<!DOCTYPE html>rn' + '<html>rn' + '<head>rn' + '<meta charset='UTF-8'>rn' + '<title>登陸網頁次數統計</title>rn' + '</head>rn' + '<body>'); out.print('<h1>'); out.print('您是第 '+ servletContext.getAttribute('count')+'位訪客'); out.print('<h1>'); out.print('</body>rn' + '</html>'); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

<%@ page contentType='text/html;charset=UTF-8' language='java' %><html> <head> <title>$Title$</title> </head> <body> <% //統計網頁訪問量 if (application.getAttribute('count') == null) { application.setAttribute('count', 0);//application.setAttribute('count', new Integer(0)); } Integer count = (Integer) application.getAttribute('count'); //使用application對象讀取count參數的值,再在原值基礎上累加1 application.setAttribute('count', count + 1);//application.setAttribute('count', new Integer(count.intValue() + 1)); %> <h2> <!-- 輸出累加后的count參數對應的值 --> 歡迎您訪問,本頁面已經被訪問過 <font color='#ff0000'><%=application.getAttribute('count')%></font>次 </h2> </body></html>

總結

到此這篇關于IDEA :當前在線人數和歷史訪問量的文章就介紹到這了,更多相關IDEA :當前在線人數和歷史訪問量內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: IDEA
相關文章:
主站蜘蛛池模板: 最新精品在线视频 | 黄色免费三级 | 免费人成黄页网站在线观看 | 末满18以下勿进色禁网站 | chineseoldman色老头videos | 免费又黄又爽又猛大片午夜 | 国产亚洲精品精品国产亚洲综合 | 欧美又粗又硬又大久久久 | 国产三级a三级三级天天 | 成人网视频免费播放 | 国产三级做爰在线观看∵ | 亚洲成人手机在线 | 欧美另类孕交 | 精品视自拍视频在线观看 | 亚欧人成精品免费观看 | 精品国产v| 美女视频黄的免费视频网页 | 正在播放亚洲一区 | 久久国产精品免费 | 成人公开免费视频 | 久草在线最新视频 | 亚洲国产欧洲综合997久久 | 超清波多野结衣精品一区 | 欧美日一本 | 亚洲一片 | 在线视频一区二区日韩国产 | 韩国免费毛片在线看 | 国内外成人免费在线视频 | 日本成人免费在线观看 | 精品丝袜国产自在线拍亚洲 | 亚洲欧美日韩在线不卡中文 | 欧美高清一级片 | 久久这里只有精品免费视频 | 成人a毛片手机免费播放 | 国内久久精品 | 亚洲欧美一区二区三区在线播放 | 欧美高清一区二区三区欧美 | 99视频在线观看高清 | 日韩日韩日韩手机看片自拍 | 久久国产亚洲观看 | a站在线观看 |