您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關利用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--,重新設置 */ @WebListener public 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> <h2>當前在線人數:${count}</h2> </body> </html>
歷史訪問量
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>\r\n" + "<html>\r\n" + "<head>\r\n" + "<meta charset=\"UTF-8\">\r\n" + "<title>登陸網頁次數統計</title>\r\n" + "</head>\r\n" + "<body>"); out.print("<h2>"); out.print("您是第 "+ servletContext.getAttribute("count")+"位訪客"); out.print("<h2>"); out.print("</body>\r\n" + "</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)); %> <h3> <!-- 輸出累加后的count參數對應的值 --> 歡迎您訪問,本頁面已經被訪問過 <font color="#ff0000"><%=application.getAttribute("count")%></font>次 </h3> </body> </html>
看完上述內容,你們對利用IDEA 實現顯示當前在線人數和歷史訪問量功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。