JSP九大隐式对象归纳总结

 首先理清一下什么是隐式对象:在编写JSP时,不需要声明(创建)就可以直接使用的对象称为隐式对象。

对应的作用如下图所示:


每个对象对应作用域如下所示:

内置对象 代表内容                                                                            范围

request 触发服务调用的请求                                                            request

response 对请求的应答                                                                    page

session 为请求的客户创建的session对象                                    session

application 从 servlet 配置对象获得的 servlet 上下文                    application

          (如在getServletConfig() ,getContext()的调用中)                                                                                                                                                               

扫描二维码关注公众号,回复: 507202 查看本文章

out 向输出流写入内容的对象                                                             page

pageContext 本 JSP 的页面上下文                                             page

page 实现处理本页当前请求的类的实例                                    page

config 本 JSP 的 ServletConfig                                                     page

exception 表示JSP页面运行时产生的异常                                      Page

代码示例:

 一:比较pagecontext,request,session,application作用域:

index1.jsp关键代码:

<%
	//存储
	pageContext.setAttribute("a", 1);
	request.setAttribute("request", 1);
	session.setAttribute("session", 1);
	application.setAttribute("application", 1);	
	//在本页获取
	Object obj = pageContext.getAttribute("a");
	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/>

 运行:


 可以看出,在本页中四个参数都能得到数据,下面我们跳转到另一个界面进行测试

在index1.jsp中增加如下代码:

<%
	request.getRequestDispatcher("index2.jsp").forward(request, response);

	//response.sendRedirect("index2.jsp");

%>

index2.jsp:

	<%
		//在转发后的页获取
		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/>

 这里注意到index1.jsp增加的代码中有两种跳转方法,一种是request.getRequestDispatcher() 另一种是response.sendRedirect(),两种分别运行效果如下(先后顺序):


可以得出结论: request.getRequestDispatcher()是请求转发,前后页面共享一个request ; 

response.sendRedirect()是重新定向,前后页面不是一个request。

并且,pagecontext只对本页有效,request在一个请求内有效。

下面测试session的作用域:

index3.jsp:

	<%
		//在转发后的页获取
		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/>

 打开IE浏览器,运行index3.jsp,与之前谷歌浏览器的index3.jsp运行结果对比:



 可以看到,session的作用域仅限于同一个浏览器(一个浏览器对应一个session),即对应一次会话。

然而application依旧是存在的,这里我们重启服务器再运行index3.jsp:


 可以发现,现在application也没了,由此可以得出结论,application的作用域最广,为服务器启动到关闭的整段时间, 在这个作用域内设置的信息可以被所有应用程序使用。 

二:分析page,config,exception,out

out应该不用分析了...就是在Jsp里的输出流。。想必都懂..

首先是page,这里编写index4.jsp:

	<%
		//由于JSP本身就是一个Servlet
		HttpJspPage servlet = (HttpJspPage) page;
		String str = servlet.getServletInfo();
	
		String str2 = this.getServletName();
		
	%>
	<%=str%>  <%=str2%>

 运行结果:



可以看到输出了当前JSP页面的配置信息,由此可以得出结论page相当于servlet中的this对象。

然后测试config

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

 运行时URL改成:http://127.0.0.1:8080/ysdx/aa

运行结果:


 可以看到Config为Jsp配置信息

最后是exception:

新建一个index6.jsp

	<%
		int a = 1, b = 0;
	%>
	<%=a / b%>
	<a href="index7.jsp">这是一个超连接</a>

 可以看到,里面发生了除0错误

新建error.jsp:

你访问的页面飞走了!!

<%
String msg = exception.getMessage();
%>

<%=msg %>

 运行inde6.jsp:

 
 可以看到报错页面是error.jsp,由此可见exception的作用是异常处理。

学习完感觉所有语言都有种触类旁通的意思,所以还是耐心学精一门语言,学其他语言都会容易上手。
 

猜你喜欢

转载自545283613.iteye.com/blog/2279980