亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:5日期: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
相關文章:
主站蜘蛛池模板: 国产在线欧美日韩一区二区 | 国语偷拍视频在线观看 | 久久爰www免费人成 久久狠狠干 | 亚洲最新黄色网址 | 久久免费观看国产精品88av | 国产色婷婷精品综合在线观看 | 真实国产乱子伦对白视频37p | 国产亚洲综合视频 | 久久青青草原精品老司机 | www国产精品com | 最新69成人精品毛片 | 大陆一级毛片免费高清 | 精品五夜婷香蕉国产线看观看 | 欧美成人免费全部观看天天性色 | 精彩视频一区二区 | 中文字幕日本亚洲欧美不卡 | 男女无遮挡边做边吃视频免费 | 午夜宅男宅女的免费网站 | 日韩一区二区三区不卡视频 | 免费一级黄色大片 | 欧美a级黄色片 | 欧美在线视频一区 | www.黄色在线 | 精彩视频一区二区三区 | 青草精品 | 美国一级毛片∞ | 成年大片免费视频播放手机不卡 | 欧美一区二区三区久久久人妖 | 青青草久热精品视频在线观看 | 欧美日韩一区二区三区四区在线观看 | 亚洲成人在线网站 | 国内黄色一级精品 | 青青青草网站免费视频在线观看 | 国产精品成人va在线观看入口 | 亚洲国产99在线精品一区二区 | 国产中的精品一区的 | 久久精品国产精品青草 | 一级一级女人18毛片 | 国产普通话对白精品hd | 激情六月丁香婷婷 | 日韩一级a毛片欧美一级 |