JSP的隐式对象[9个]
1》用来数据共享的对象:
pageContext:?
request:?
session:?
application:?
2》和Servlet有关的对象:
page:?
config:
?
3》与输入输出有关的对象:
out?:
request:
response:
?
4》和异常处理有关的对象
exception?
下面便来一一讲解:
?
?
1》用来数据共享的对象:
数据共享(数据范围从小到大):
???? pageContext(在网页本页中共享数据)
????? request (在同一次请求响应过程中)
????? session?(在同一个会话中共享数据)
????? application(应用程序运行期间共享数据)
这4个容器对象都可以用来存数据,方式一样:
<%@ 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>数据共享</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>
?
页面输出:
pageContext:1
?request:1
?session:1
?application:1
?
插入一点重要内容:
Servlet转发机制
a).forword转发request.getRequestDispatcher("index.jsp").forward(request, response);
b).include转发request.getRequestDispatcher("index.jsp").include(request,response);
c).重定向转发response.sendRedirect("index.jsp");
(重定向转发与上面两种区别,重新发送请求,request里面共享的数据不再存在)
?
?
2》和Servlet有关的对象:
page:page和servlet有关的,指jsp页面本身(jsp就是一个servlet),page就是this,想用page的地方都可以用this来表示;所以page使用少;
config:存储jsp配置信息的对象:
xml文件配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ysdx</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
?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>
?
3》与输入输出有关的对象:
out:向浏览器输出信息
request:包含请求信息
response:包含的响应信息
?
4.和异常有关的对象:
exception:异常处理(只有在声明为错误页面才有exception对象),
??1)定义错误页面
??<%@ page isErrorPage="true" %>
-----------------------------------------------
<%@ 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>
?2)如果页面需要处理异常
??<%@ page errorPage="error.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>
?
?
?
原文:http://qq-24665727.iteye.com/blog/2279609