您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关利用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。