JSP_<jsp:application>实例

本文出自: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));
 %>


2.test_application.jsp

<%@ 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值