JSP内置对象概览
NO | 内置对象 | 类型 | 描述 |
1 | pageContext | javax.servlet.jsp.PageContext | JSP页面容器 |
2 | request | javax.servlet.http.HttpServletRequest | 得到用户的请求信息 |
3 | response | javax.servlet.http.HttpServletResponse | 服务器向客户端的回应信息 |
4 | session | javax.servlet.http.HttpSession | 用来保存每一个用户的信息 |
5 | application | javax.servlet.ServletContext | 表示所有用户的共享信息 |
6 | config | javax.servlet.ServletConfig | 服务器配置,可以得到初始化参数 |
7 | out | javax.servlet.jsp.JspWriter | 页面输出 |
8 | page | java.lang.Object | 表示从该页面生成的一个servlet实例 |
9 | exception | java.lang.Throwable | 表示JSP页面发生的异常,错误页有效 |
JSP 4种属性范围
1.page:只在一个页面中保存属性,跳转后失效.
2.request:只在一次请求中保存,服务器跳转后依然有效
3.session:在一次会话范围中,无论何种跳转都可以使用,但是新开的浏览器无法使用.
4.application:在整个服务器上保存,所有用户都可以使用.
page属性(pageContext)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>get and set pageContext Attribute</title> 9 </head> 10 <body> 11 <%//设置页面属性,该属性仅在本页有效 12 pageContext.setAttribute("name", "hx"); 13 pageContext.setAttribute("birthday",new Date()); 14 %> 15 <%//获得页面属性,并向下转型 16 String name = (String)pageContext.getAttribute("name"); 17 Date d = (Date) pageContext.getAttribute("birthday"); 18 %> 19 <%//显示页面属性值 20 out.println(name); 21 out.println(d); 22 %> 23 24 </body> 25 </html>
在本页中正常显示.修改使其发生服务器跳转.
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>get and set pageContext Attribute</title> 9 </head> 10 <body> 11 <%//设置页面属性,该属性仅在本页有效 12 pageContext.setAttribute("name", "hx"); 13 pageContext.setAttribute("birthday",new Date()); 14 %> 15 <%//获得页面属性,并向下转型 16 String name = (String)pageContext.getAttribute("name"); 17 Date d = (Date) pageContext.getAttribute("birthday"); 18 %> 19 <%//显示页面属性值 20 out.println(name); 21 out.println(d); 22 %> 23 <jsp:forward page="2getAndSetPageAttribute.jsp"/> 24 25 </body> 26 </html>
1 <!-- 2getAndSetPageAttribute.jsp --> 2 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <%@ page import="java.util.*" %> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <%//获得页面属性,并向下转型 14 String name = (String)pageContext.getAttribute("name"); 15 Date d = (Date) pageContext.getAttribute("birthday"); 16 %> 17 <%//显示页面属性值 18 out.println(name); 19 out.println(d); 20 %> 21 </body> 22 </html>
在发生跳转后,name和d均为null,pageContext仅本页有效
request属性范围
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>request attribute scope</title> 9 </head> 10 <body> 11 <% 12 request.setAttribute("name", "hx"); 13 request.setAttribute("birthday",new Date()); 14 %> 15 <% 16 String name=(String)request.getAttribute("name"); 17 Date d = (Date)request.getAttribute("birthday"); 18 %> 19 <% 20 out.println(name); 21 out.println(d); 22 %> 23 </body> 24 </html>
本页生效
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>request attribute scope</title> 9 </head> 10 <body> 11 <% 12 request.setAttribute("name", "hx"); 13 request.setAttribute("birthday",new Date()); 14 %> 15 <% 16 String name=(String)request.getAttribute("name"); 17 Date d = (Date)request.getAttribute("birthday"); 18 %> 19 <% 20 out.println(name); 21 out.println(d); 22 %> 23 <jsp:forward page="request_scope2.jsp"></jsp:forward> 24 </body> 25 </html> 26 27 request_scope2.jsp 28 <%@ page language="java" contentType="text/html; charset=UTF-8" 29 pageEncoding="UTF-8"%> 30 <%@ page import="java.util.*" %> 31 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 32 <html> 33 <head> 34 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 35 <title>Insert title here</title> 36 </head> 37 <body> 38 <% 39 String name=(String)request.getAttribute("name"); 40 Date d = (Date)request.getAttribute("birthday"); 41 %> 42 <% 43 out.println(name); 44 out.println(d); 45 %> 46 </body> 47 </html>
服务器跳转生效
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>request attribute scope</title> 9 </head> 10 <body> 11 <% 12 request.setAttribute("name", "hx"); 13 request.setAttribute("birthday",new Date()); 14 %> 15 <% 16 String name=(String)request.getAttribute("name"); 17 Date d = (Date)request.getAttribute("birthday"); 18 %> 19 <% 20 out.println(name); 21 out.println(d); 22 %> 23 <a href="request_scope2.jsp">客户端跳转</a> 24 </body> 25 </html> 26 27 28 request_scope2.jsp 29 30 <%@ page language="java" contentType="text/html; charset=UTF-8" 31 pageEncoding="UTF-8"%> 32 <%@ page import="java.util.*" %> 33 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 34 <html> 35 <head> 36 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 37 <title>Insert title here</title> 38 </head> 39 <body> 40 <% 41 String name=(String)request.getAttribute("name"); 42 Date d = (Date)request.getAttribute("birthday"); 43 %> 44 <% 45 out.println(name); 46 out.println(d); 47 %> 48 </body> 49 </html>
使用超连接(客户端跳转后)属性值无效.
session属性范围
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 session.setAttribute("name", "hx"); 13 session.setAttribute("birthday",new Date()); 14 %> 15 <% 16 String name = (String)session.getAttribute("name"); 17 Date d = (Date)session.getAttribute("birthday"); 18 %> 19 <% 20 out.println(name); 21 out.println(d); 22 %> 23 24 </body> 25 </html>
本页有效
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% session.setAttribute("name", "hx"); session.setAttribute("birthday",new Date()); %> <% String name = (String)session.getAttribute("name"); Date d = (Date)session.getAttribute("birthday"); %> <% out.println(name); out.println(d); %> <a href="session_scope2.jsp">test session scope</a> </body> </html> session_scope2.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name = (String)session.getAttribute("name"); Date d = (Date)session.getAttribute("birthday"); %> <% out.println(name); out.println(d); %> </body> </html>
客户端转发也有效
application
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% application.setAttribute("name", "hx"); application.setAttribute("birthday",new Date()); %> <% String name = (String)application.getAttribute("name"); Date d = (Date)application.getAttribute("birthday"); %> <% out.println(name); out.println(d); %> <a href="applicaion_scope2.jsp">test application scope</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name = (String)application.getAttribute("name"); Date d = (Date)application.getAttribute("birthday"); %> <% out.println(name); out.println(d); %> </body> </html>
在A浏览器中访问第一段代码所在URL,然后在B浏览器中访问第二段代码所在URL,显示正常
重访PageContext:
在page.setAttribute()方法是一个重写方法.
一个是public void setAttribute(String name, Object value)
一个是public void setAttribute(String name, Object value, int scope)
在此方法中的int scope相当于选择范围,可选的数为
1.public void final int PAGE_SCOPE; 表示page属性范围
2.public void final int REQUEST_SCOPE; 表示request属性范围
3.public void final int SESSION_SCOPE; 表示session属性范围
4.public void final int APPLICATION_SCOPE; 表示application属性范围
pageContext对象可以随意设置属性范围,默认为page属性,而其他操作是对这一功能的包装.习惯上仅让pageContext具有page属性范围