您的位置:首页 > 移动开发

有关JSP的几个内置对象——session/application/pageContext

2011-01-11 21:35 656 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	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>My JSP 'index.jsp' starting page</title>
		<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" mce_href="styles.css">
	-->
	</head>

	<body> 
		<br>
		<%--if(u.equals("admin"))
       out.println(u);
       else out.println("error");--%>
		<%
			String u = request.getParameter("user");
			out.println(u);
		%>
		<%
			if (pageContext.getAttribute("pageCount") == null) {
				pageContext.setAttribute("pageCount", new Integer(0));
			}
			if (session.getAttribute("sessionCount") == null) {
				session.setAttribute("sessionCount", new Integer(0));
			}
			if (application.getAttribute("appCount") == null) {
				application.setAttribute("appCount", new Integer(0));
			}
			if (request.getAttribute("reqCount") == null) {
				request.setAttribute("reqCount", new Integer(0));
			}
		%>
		<h1>
			session、application 和 pageContext
		</h1>
		<%
			Integer count = (Integer) pageContext.getAttribute("pageCount");
			pageContext.setAttribute("pageCount", new Integer(
					count.intValue() + 1));
			Integer count2 = (Integer) session.getAttribute("sessionCount");
			session.setAttribute("sessionCount", new Integer(
					count2.intValue() + 1));

			Integer count3 = (Integer) application.getAttribute("appCount");
			application.setAttribute("appCount", new Integer(
					count3.intValue() + 1));
		%>
		<b>页面计数= </b>
		<%=pageContext.getAttribute("pageCount")%>
		<br />
		<b>会话计数= </b>
		<%=session.getAttribute("sessionCount")%>
		<br />
		<b>应用程序计数= </b>
		<%=application.getAttribute("appCount")%>
		<br />
	</body>
</html>




运行结果:

pageContent的刷新一直为1

session的刷新增长,关掉并重新开起来一个浏览器后置1

application的刷新增长,关掉server后开起来置1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐