本文出自:http://blog.csdn.net/svitter
实验环境:Myeclipse10 + tomcat7
Application对象为了多个应用程序保存信息,对于每个容器,每个用户都共同拥有一个application对象,服务器启动以后,会自动创建一个application对象,这个对象会一直保持到服务器关闭。
下列实例用于统计页面访问次数。
1.application.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% int count = 0; String c_name = request.getParameter("c_name"); try { count = Integer.parseInt((application.getAttribute("c_name").toString())); } catch(Exception e){ } out.println("自服务器启动后,此页面已经被访问了" + count +"次"); count++; application.setAttribute("c_name", new Integer(count)); %>
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8" errorPage=""%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>使用计数器的例子</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 测试计数器<br> <jsp:include page="application.jsp"> <jsp:param name="c_name" value=" " /> </jsp:include> </body> </html>
对应资源:http://download.csdn.net/detail/svitter/7358563