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

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

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

瀏覽:2日期: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
相關文章:
主站蜘蛛池模板: 一级做a爱过程免费观看 | 国产精品久久久久网站 | 欧美精品xxx| 色偷偷亚洲第一成人综合网址 | 禁止18周岁进入免费网站观看 | 特黄毛片 | 国产二区三区毛片 | 日韩特级毛片 | 中美日韩在线网免费毛片视频 | 99在线视频观看 | 免费观看呢日本天堂视频 | 成年美女黄网站色视频大全免费 | 亚洲高清在线播放 | 一区二区三区免费视频观看 | 最新69成人精品毛片 | 91久久精品国产91久久性色也 | 永久精品免费影院在线观看网站 | 日韩美女在线看免费观看 | 日本高清一本二本三本如色坊 | 国产九九精品视频 | 国产一区二区三区在线观看影院 | 看真人一一级毛片 | 国产在线观看网址你懂得 | 国产亚洲精品久久久久久久 | 国产精品视频一区二区猎奇 | 亚洲欧美日本综合一区二区三区 | 亚洲综合在线另类色区奇米 | 视频一区精品 | 国产精品久久久久毛片 | 影院成人区精品一区二区婷婷丽春院影视 | 亚洲成人一区二区 | 久久视频在线播放视频99re6 | 精品一区二区三区免费毛片爱 | 免看一级一片一在线看 | 免费国产成人综合 | 国产精品综合一区二区 | 成人a级高清视频在线观看 成人a毛片 | 国产午夜亚洲精品国产 | 亚洲精品综合一区二区 | 免费乱码中文字幕网站 | 免费一级欧美大片视频在线 |