JSP中的九大隐式对象

隐式对象 :在编写jsp时,不需要声明(创建)就可以直接使用的对象。
JSP中有九大隐式对象,分别为:  pageContext,request,session,application,page,config,out,response,exception

一.用来数据共享的对象:
pageContext: 在本页共享数据
request: 在同一次请求响应过程中共享数据
session: 在同一个会话中共享数据
application: 在应用程序运行期间共享数据
例:
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/>

</body>
</html>

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的数据<br/>
   pageContext:<%=obj %> <br/>
	request:<%=obj1 %><br/>
	session:<%=obj2 %><br/>
	application:<%=obj3 %><br/>

</body>
</html>


1 index.jsp运行的结果为:
    pageContext:null
    request:1
    session:1
    application:1

2 将从index.jsp转到index2.jsp,添加代码
request.getRequestDispatcher("index2.jsp").forward(request,response);
因为不是在一个页面中,所以
结果为:这个是index2的数据
    pageContext:null
    request:1
    session:1
    application:1
3 但将index中的转发改为forward转发,即不是在同一个请求中,即为:response.sendRedirect("index2.jsp");
结果为:这个是index2的数据
     pageContext:null
     request:null
     session:1
     application:1

4 将浏览器关闭,即不在同一个会话中时
结果为:这个是index2的数据
     pageContext:null
     request:null
     session:null
     application:1
5 结束程序,结果都为null


二.和Servlet有关的对象:
page: 指jsp页面本身
config: 用来存储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>
这个是index5.jsp!<br/>

	<%
	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>

在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>


结果为:
这个是index5.jsp!
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
scott
tiger

三.与输入输出有关的对象:
out 向浏览器输出信息
request:包含请求信息
response:包含的响应信息

四.和异常处理有关的对象
exception 用来处理异常的对象
定义错误页面
<%@ page isErrorPage="true" %>

如果页面需要处理异常
<%@ page errorPage="error.jsp"%>

猜你喜欢

转载自zxdawn.iteye.com/blog/2279997