jsp学习(4)---jsp9个内置对象
2017-04-18 16:52
169 查看
一、基本内容
1.九大内置对象主要内容对象名 | 类型 | 说明 |
request | javax.servlet.http.HttpServletRequest | |
response | javax.servlet.http.HttpServletResponse | |
session | javax.servlet.http.HttpSession | 由session="true"开关 |
application | javax.servlet.ServletContext | |
exception | java.lang.Throwable | 由isErrorPage="false"开关 |
page | java.lang.Object当前对象this | 当前servlet实例 |
config | javax.servlet.ServletConfig | |
out | javax.servlet.jsp.JspWriter | 字符输出流,相当于 printWriter对象 |
pageContext | javax.servlet.jsp.PageContext |
2.pageContext的基本使用说明
(1)基本方法
void setAttribute(String name,Object o); //用来创建域对象
Object getAttribute(String name); //用来获取域对象
void removeAttribute(String name); //移除域对象
(2)操作其他域对象
pageContext作为一个域对象的同时可以操作其他3域对象(request、response、application)数据
void setAttribute(String name,Object o,int Scope);
Object getAttribute(String name,int Scope);
voidremoveAttribute(String name,int Scope);
scpoe的值:
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE
二、request、response、application、pageContext域对象的作用范围
1.pageContext作用范围仅在当前页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <% pageContext.setAttribute("p", "pageContext"); /* 等价于下面内容 */ //pageContext.setAttribute("p", "one",PageContext.PAGE_SCOPE); %> <%=pageContext.getAttribute("p",PageContext.PAGE_SCOPE) %> </body> </html>
访问该界面为:
进行请求装发到6.jsp
在5.jsp页面中
<body> <% pageContext.setAttribute("p", "pageContext"); /* 等价于下面内容 */ //pageContext.setAttribute("p", "one",PageContext.PAGE_SCOPE); request.getRequestDispatcher("/6.jsp").forward(request, response); %> </body>
在6.jsp页面中
<body> <% String p = (String)pageContext.getAttribute("p"); out.print(p); %> </body>
得到的结果为:
结论:pageContext的作用范围仅在当前界面
2.request、Session、application作用范围比较
request的作用范围:存放的数据在一次请求(转发)内有效。使用非常多session的作用范围:存放的数据在一次会话中有效。使用的比较多。如:存放用户的登录信息,购物车功能。
application作用范围:存放的数据在整个应用范围内都有效,因为范围太大,应尽量少
在5.jsp页面中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <% pageContext.setAttribute("p", "request", PageContext.REQUEST_SCOPE); /* 等价于下面内容 */ //request.setAttribute("p", "request"); pageContext.setAttribute("p", "session", PageContext.SESSION_SCOPE); /* 等价于下面内容 */ //session.setAttribute("p", "session"); pageContext.setAttribute("p", "application", PageContext.APPLICATION_SCOPE); /* 等价于下面内容 */ //application.setAttribute("p", "request"); //request.getRequestDispatcher("/6.jsp").forward(request, response); response.sendRedirect(request.getContextPath()+"/6.jsp"); %> </body> </html>
在6.jsp页面中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <% out.print(request.getAttribute("p")); %> <%=session.getAttribute("p") %> <%=application.getAttribute("p") %> </body> </html>
得到的结果是:
结论:request、Session、application的作用范围为:application>session>request
三、PageContext的其他用法
findAttribute(String name); 自动从page request sessionapplication依次查找,找到了就取值,结束查找。修改6.jsp页面的内容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <%=pageContext.findAttribute("p") %> </body> </html>
得到的结果为:
四、总结
以上便是对jsp九大内置对象的总结,如果中间有任何错误请提出,谢谢!
相关文章推荐
- JSP中的内置对象(9个)
- JSP的3个编译指令,7个动作指令,9个内置对象
- Jsp的9个内置对象分析
- JSP9个内置对象
- JSP的9个内置对象-session
- JSP系列四:JSP9个内置对象
- JSP脚本的9个内置对象(三)——exception对象
- JSP脚本中的9个内置对象
- jsp内置的9个对象的应用
- jsp的9个内置对象及各自的作用?
- Jsp9个内置对象详解
- JSP的9个内置对象
- JSP脚本的9个内置对象(二)——config对象
- JSP的9个内置对象-application
- JSP的9个内置对象
- JSP脚本中的9个内置对象
- JSP脚本的9个内置对象(一)——简述、Application对象
- JSP的9个常用内置对象详解
- JSP的9个内置对象-response
- JSP 9个内置对象