问题引入:
网站需要实现一个简单的网页计数器.效果图如下:
什么是Application:
application(应用对象):存活范围最大的对象,只要服务器没有关闭,application对象中的数据就会一直存在。在整个服务器运行过程中,application对象只有一个
request、session以及application这3个对象的范围是逐个增加的:request只在一个请求的范围内,session是在浏览器窗口的范围内。application则是在整个服务器运行过程中。
应用代码:
<%
if(application.getAttribute("counter")==null)
{
application.setAttribute("counter", "1");
}
else
{
String strnum=null;
strnum =(String)application.getAttribute("counter");
int icount=0;
icount=Integer.valueOf(strnum).intValue();
icount++;
application.setAttribute("counter",Integer.toString(icount));
}
%>
您是第<%=application.getAttribute("counter") %>位访问者;
项目案例:
index.jsp(代码) 页面:
hots.jsp(代码) 页面:
hots.jsp(视图) 展示:
总结:
要实现一个网页计数器并不难,仅仅在JSP页面使用<%%>嵌套Java代码也可以实现.