[jsp website counting function] application session
4889 단어 application
count.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%!
synchronized void countPeople(){
ServletContext application = this.getServletContext();
Integer number = (Integer) application.getAttribute("count");
if(number == null){
number = new Integer(1);
application.setAttribute("count", number);
}
else{
number = new Integer(number.intValue() + 1);
application.setAttribute("count",number);
}
}
%>
<%
if(session.isNew()){
countPeople();
}
Integer yourNumber = (Integer) application.getAttribute("count");
%>
<p></p>
, <%= yourNumber %> 。
</body>
</html>
In the above code, in order to avoid concurrency problems with the application object, the synchronized keyword is used. In order to avoid the page refresh problem, the session.isNew() judgment is added. If the session object is newly created, it indicates that the user starts to access the system and the counter is incremented by 1; when the user refreshes the current page, since the session object is not newly created, it is avoided. Repeat the problem of adding 1
In fact, when implementing the counter function, you need to pay attention to some details, such as frequent refresh and concurrent access. Since the life cycle of the application object is related to server start and stop, if the server restarts due to a problem, the counters saved by the application will be cleared. To solve this problem, you can save the value of the counter in a file, and then add 1 to the counter by using the function of reading and writing files.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.