JSP中9大隐式对象的总结
JSP的隐式对象[9个]
用来数据共享的对象:
pageContext: 在本页共享数据
request: 在同一次请求响应过程中共享数据
session: 在同一个会话中共享数据
application: 在应用程序运行期间共享数据
和Servlet有关的对象:
page: 指jsp页面本身
config: 用来存储jsp配置信息的对象
与输入输出有关的对象:
out 向浏览器输出信息
request:包含请求信息
response:包含的响应信息
和异常处理有关的对象
exception 用来处理异常的对象
定义错误页面
<%@ page isErrorPage="true" %>
如果页面需要处理异常
<%@ page errorPage="error.jsp"%>
request 请求对象 类型 javax.servlet.ServletRequest 作用域 Request
response 响应对象 类型 javax.servlet.SrvletResponse 作用域 Page
pageContext 页面上下文对象 类型 javax.servlet.jsp.PageContext 作用域 Page
session 会话对象 类型 javax.servlet.http.HttpSession 作用域 Session
application 应用程序对象 类型 javax.servlet.ServletContext 作用域 Application
out 输出对象 类型 javax.servlet.jsp.JspWriter 作用域 Page
config 配置对象 类型 javax.servlet.ServletConfig 作用域 Page
page 页面对象 类型 javax.lang.Object 作用域 Page
exception 例外对象 类型 javax.lang.Throwable 作用域 Page 来
代码示例index.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("pageContext", 1);
request.setAttribute("request", 1);
session.setAttribute("session", 1);
application.setAttribute("application", 1);
//在本页获取
Object obj = pageContext.getAttribute("pageContext");
Object obj1 = request.getAttribute("request");
Object obj2 = session.getAttribute("session");
Object obj3 = application.getAttribute("application");
%>
pageContext:<%=obj %><br/>
request:<%=obj1 %><br/>
session:<%=obj2 %><br/>
application:<%=obj3 %><br/>
<%
//request.getRequestDispatcher("index2.jsp").forward(request, response);
//response.sendRedirect("index2.jsp");
%>
</body>
</html>
这是index.jsp文件,在小文本程序脚本中分别给pageContext,request,session,application三个对象赋值1,并且在本页打出,页面显示:
pageContext:1
request:1
session:1
application:1
四个对象中的值,
打开index.jsp中的
request.getRequestDispatcher("index2.jsp").forward(request, response);
让改页面跳转到到index2.jsp.
下面给出index2.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>
<%
//在转发后的页获取
Object obj = pageContext.getAttribute("pageContext");
Object obj1 = request.getAttribute("request");
Object obj2 = session.getAttribute("session");
Object obj3 = application.getAttribute("application");
%>
这个是index2.jsp的数据:<br/>
pageContext:<%=obj %><br/>
request:<%=obj1 %><br/>
session:<%=obj2 %><br/>
application:<%=obj3 %><br/>
<%
//request.getRequestDispatcher("index3.jsp").forward(request, response);
%>
</body>
</html>
再次执行index.jsp显示的结果:
这个是index2.jsp的数据:
pageContext:null
request:1
session:1
application:1
由此说明pageContext的作用范围是本页中。
这一次注销index.jsp中的//request.getRequestDispatcher("index2.jsp").forward(request, response);
打开response.sendRedirect("index2.jsp");
显示运行结果:
这个是index2.jsp的数据:
pageContext:null
request:null
session:1
application:1
这是什么原因呢?
这是因为forward和重定向跳转有区别,forward跳转是同一个请求响应,重定向跳转是进行了第二次请求响应,当使用重定向跳转进行了新的请求响应,由此也说明了request是作用范围是在同一个请求响应中。并且request的作用范围大于pageContext。
接下来打开index.jsp中的
request.getRequestDispatcher("index2.jsp").forward(request, response);
关闭
//response.sendRedirect("index2.jsp");
然后打开index2.jsp中的
request.getRequestDispatcher("index3.jsp").forward(request, response);
运行index.jsp运行结果显示:
这个是index33333333.jsp的数据:
pageContext:null
request:1
session:1
application:1
下面给出index3.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>
<%
//在转发后的页获取
Object obj = pageContext.getAttribute("pageContext");
Object obj1 = request.getAttribute("request");
Object obj2 = session.getAttribute("session");
Object obj3 = application.getAttribute("application");
%>
这个是index33333333.jsp的数据:<br/>
pageContext:<%=obj %><br/>
request:<%=obj1 %><br/>
session:<%=obj2 %><br/>
application:<%=obj3 %><br/>
</body>
</html>
这里我们需要注意:同一浏览器无论多少窗口访问同一个地址,那么他们都是同一个session对象,但是不同浏览器访问同一地址,是不同的session对象。
当我们在同一个浏览器里面运行index.jsp页面,也就是给那四个对象赋值,不管我们打开多少次窗口运行多少次index2.Jsp页面结果显示也是:
这个是index33333333.jsp的数据:
pageContext:null
request:null
session:1
application:1
但是当我们在其他的浏览器中访问index2.jsp或者关闭浏览器重新访问index2.jsp的时候页面显示的结果:
这个是index33333333.jsp的数据:
pageContext:null
request:null
session:null
application:1
在此证明了session的作用范围是一次会话。
其实也另外说明从index.jsp跳转index2.jsp,再从index2.jsp跳转index3.jsp是同一次请求应答,它与index2.jsp直接跳转index3.jsp不是同一个请求应答。
这一次我们关掉服务器重新启动Tomcat,再去访问index2.jsp或者index3.jsp显示结果:
这个是index33333333.jsp的数据:
pageContext:null
request:null
session:null
application:null
这里说明application的作用范围是在应用程序运行期间。
下面给出index4.jsp的代码:
<%@page import="org.apache.jasper.servlet.JspServlet"%>
<%@ 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>
<%
//由于JSP本身就是一个Servlet
HttpJspPage servlet = (HttpJspPage) page;
String str = servlet.getServletInfo();
String str2 = this.getServletName();
String str3=this.getServletInfo();
%>
<%=str%> -- <%=str2%>--<%=str3%>
</body>
</html>
其实我觉得这里的重点是强调jsp本身就是一个Servlet,我就简单说一下jsp怎么工作的,Tomcat现将jsp转化成jsp.java 再将jsp.java转化成jsp.class文件运行
运行结果显示:
Jasper JSP 2.1 Engine -- jsp--Jasper JSP 2.1 Engine
str和str3一样很明显说明了page值jsp页面本身。
下面给出index5.jsp的代码:
<%@page import="org.apache.jasper.servlet.JspServlet"%>
<%@ 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>
这个是index5.jsp!
<%
String className = config.getInitParameter("className");
String url = config.getInitParameter("url");
String user = config.getInitParameter("user");
String pwd = config.getInitParameter("pwd");
%>
<%=className %><br/>
<%=url %><br/>
<%=user %><br/>
<%=pwd %><br/>
</body>
</html>
运行结果显示:
这个是index5.jsp! oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
scott
tiger
这是因为我们在web.xml中配置了。
<servlet>
<servlet-name>aa</servlet-name>
<jsp-file>/index5.jsp</jsp-file>
<init-param>
<param-name>className</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@127.0.0.1:1521:ORCL</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>scott</param-value>
</init-param>
<init-param>
<param-name>pwd</param-name>
<param-value>tiger</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>aa</servlet-name>
<url-pattern>/aa</url-pattern>
</servlet-mapping>
所以config是用来存储jsp配置信息的对象
Out向浏览器输出信息我们经常会用到
PrintWriter out = response.getWriter();
out.print(“ ”);
我们最后一个说一下这个异常对象
exception 用来处理异常的对象
下面给出index6.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp"%>
<!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>
<%
int a = 1, b = 0;
%>
<%=a / b%>
<a href="index7.jsp">这是一个超连接</a>
</body>
</html>
还有error.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!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 msg = exception.getMessage();
%>
<%=msg %>
</body>
</html>
运行index6.jsp的结果显示:
你访问的页面飞走了!! / by zero
要注意的就是上面我们说道的需要定义错误页面,并且在出错的页面中写入
<%@ page errorPage="error.jsp"%>
阅读更多- JSP学习总结-jsp的9大隐式对象的使用总结
- jsp的9大隐式对象的使用总结
- JSP中为何内置对象——JSP中九个内置对象使用总结
- 面试总结之JSP原理、内置对象以及作用域
- jsp常用的九个内置对象总结
- JSP内置9大对象学习总结(二)
- JSP整理总结--9大内置对象、4个作用域
- EL表达式-11大隐式对象---张国亮总结心得第三季
- JSP九大内置对象的作用和用法总结?
- JSP九大内置对象的作用和用法总结(转)
- 大数据WEB阶段(十三)JSP(一)JSP基础、JSP指令详解、四大域九大隐式对象总结
- JSP内置9大对象学习总结(三)
- JSP九大内置对象的作用和用法总结
- jsp学习与总结(九大内置对象)
- 关于Jsp中的几个域对象的学习总结
- JSP九大内置对象 四大作用域 自我总结
- Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读
- JavaWeb---总结(十七)JSP中的九个内置对象
- JavaWeb总结十七、JSP中的九个内置对象
- JSP九大内置对象的作用和用法总结?