<%@page?import="java.util.ArrayList"%>
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"
????pageEncoding="UTF-8"%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
<title>Insert?title?here</title>
</head>
<body>
????<%
????????String?str?=?"el";
????????request.setAttribute("s",?str);
????????request.setAttribute("st.str",?"demo");
????????ArrayList?al?=?new?ArrayList();
????????al.add("test1");
????????al.add("test2");
????????request.setAttribute("al",al);
????????Integer?count?=?(Integer)application.getAttribute("count");
????????if(count?==?null)?{
????????????application.setAttribute("count",?1);
????????}else{
????????????application.setAttribute("count",?count?+?1);
????????}
????%>
????${s?}<br/>
????${requestScope["st.str"]?}<br/>
????${al[1]?}<br/>
????瀏覽次數:${applicationScope.count?}
</body>
</html>代碼如上,用application對象統計瀏覽次數時,并不是從一開始,第一次進去就顯示2.請問哪里錯了
1 回答

快樂的時光
TA貢獻79條經驗 獲得超48個贊
第一次顯示的是1,你說的二估計是你關閉了瀏覽器,重新開啟了。
application是全局變量,除非tomcat關閉,它就會一直在內存當中。不會消失。
代碼沒有錯誤。可能是application的作用范圍還沒深刻理解。
希望對你有幫助。
你可以重啟tomcat,再試一下。
添加回答
舉報
0/150
提交
取消