一:page属性范围
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘JspObj.jsp‘ starting page</title>
</head>
<body>
<h1>page的属性范围</h1>
<%
//这些属性只能在当前jsp页面有用
pageContext.setAttribute("name", "China");
pageContext.setAttribute("history","5000");
%>
<%
//取出属性,并向下转型
Stringcityname=(String)pageContext.getAttribute("name");
Stringcityhistory=(String)pageContext.getAttribute("history");
%>
<h5>国家名称:<%=cityname %></h5>
<h5>国家历史:<%=cityhistory %></h5>
<hr>
</body>
</html>
二:request属性范围
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘JspObj.jsp‘ starting page</title>
</head>
<body>
<h1>request的属性范围</h1>
<%
//request的属性,只在服务器跳转中起作用
request.setAttribute("req", "request属性");
request.setAttribute("reuse","只在服务器跳转中起作用");
%>
<jsp:forward page="Demo3.jsp"/>
</body>
</html>
Demo3.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘Demo3.jsp‘ starting page</title>
</head>
<body>
<h1>获取request对象属性</h1>
<%
Stringreque=(String)request.getAttribute("req");
Stringreuse=(String)request.getAttribute("reuse");
%>
<h5>对象名:<%=reque %></h5>
<h5>用法:<%=reuse %></h5>
</body>
</html>
三:session属性范围
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘JspObj.jsp‘ starting page</title>
</head>
<body>
<h1>session的属性范围</h1>
<%
session.setAttribute("sename","session属性范围");
session.setAttribute("seuse","在一个浏览器中始终有效");
%>
<!-- 地址改变,客户端跳转 -->
<a href="Demo4.jsp">超链接获取属性</a>
<hr>
</body>
</html>
Demo4.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘Demo4.jsp‘ starting page</title>
</head>
<body>
<h1>获取session对象属性</h1>
<%
Stringsename=(String)session.getAttribute("sename");
Stringseuse=(String)session.getAttribute("seuse");
%>
<h5>对象名:<%=sename %></h5>
<h5>用法:<%=seuse %></h5>
</body>
</html>
四:application属性范围
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘JspObj.jsp‘ starting page</title>
</head>
<body>
<h1>application的属性范围</h1>
<%
application.setAttribute("apname","application属性范围");
application.setAttribute("apuse","设置在服务器端");
%>
<!-- 地址改变,客户端跳转 -->
<a href="Demo4.jsp">超链接获取属性</a>
<hr>
</body>
</html>
Demo4.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘Demo4.jsp‘ starting page</title>
</head>
<body>
<h1>获取application对象属性</h1>
<%
Stringapname=(String)application.getAttribute("apname");
Stringapuse=(String)application.getAttribute("apuse");
%>
<h5>对象名:<%=apname %></h5>
<h5>用法:<%=apuse %></h5>
</body>
</html>
这些是很基础的概念,自己还要多看书籍,开阔知识面!
QQ:1327880701
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21