这是正经的,但是效果不明显(建议每运行一次,就换个浏览器,观察次数)
<body>
<form action=Myjsp.jsp method="post">
<table border=1>
<tr><td align="center" colspan="2">用户登陆页面</td></tr>
<tr><td>用户名:</td><td><input type="text" name="name" size="29"></td></tr>
<tr><td>密码:</td><td><input type="password" name="password" size="30"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="登陆"></td></tr>
</table>
</form>
<%! synchronized void count() {
ServletContext application = getServletContext();
Integer num = (Integer)application.getAttribute("num");
if (num == null) {
num = new Integer(1);
application.setAttribute("num", num);
} else {
num = new Integer(1 + num);
application.setAttribute("num", num);
}
} %>
<%
if (session.isNew()) {
count();
}
Integer Num = (Integer)application.getAttribute("num");
%>
第<%=Num %>个访问
</body>
不正经但是,效果明显的 注意定义了新对象application1 在一个浏览器就可以看到效果
<body>
<form action=Myjsp.jsp method="post">
<table border=1>
<tr><td align="center" colspan="2">用户登陆页面</td></tr>
<tr><td>用户名:</td><td><input type="text" name="name" size="29"></td></tr>
<tr><td>密码:</td><td><input type="password" name="password" size="30"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="登陆"></td></tr>
</table>
</form>
<%
ServletContext application1 = getServletContext();
Integer num = (Integer)application1.getAttribute("num");
if (num == null) {
num = new Integer(1);
application1.setAttribute("num", num);
} else {
num = new Integer(1 + num);
application1.setAttribute("num", num);
} %>
<%
count();
Integer Num = (Integer)application1.getAttribute("num");
%>
第<%=Num %>个访问
</body>