This article illustrates the way JSP uses application to count online numbers. Share to everyone for your reference.
The implementation methods are as follows:
Copy Code code 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" >
<title>application</title>
<body>
<%!
Integer number;//
synchronized void Numbervisiter ()
{
ServletContext application = Getservletcontext ();
Integer num = (integer) application.getattribute ("Count");
if (num = null)//If the first visitor
{
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 user
</font>
</p>
</body>
I hope this article will help you with the JSP program design.