jsp's method of counting online population using application

  • 2021-06-29 11:46:38
  • OfStack

This article gives an example of how jsp uses application to count online people.Share it for your reference.

The implementation is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>application</title>
  </head>
  
  <body>
  <%!
Integer number;//
synchronized void numberVisiter()
{
ServletContext application = getServletContext();
Integer num = (Integer)application.getAttribute("count");
if(num == null)// If yes 1 Visitors
{
num = new Integer(1);
application.setAttribute("count",num);
}
else
{
num = new Integer(num.intValue() + 1);
application.setAttribute("count",num);
}
}
  %>
  <%
  if(session.isNew())
  {
  numberVisiter();
  Integer number = (Integer)application.getAttribute("count");
  }
  %>
  <p>
  <font size="2" color="blue"> Simple Page Access Counter </font>
  </p>
  <p>
  <font size="2" color="#000000">
  Welcome to this page, you are <%=number%> Access Users
  </font>
  </p>
  </body>
</html>

I hope that the description in this paper will be helpful to everyone's jsp program design.


Related articles: