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>
相关推荐
了解JSP 隐式对象的分类和组成 能够运用隐式对象进行JSP 编程 理解Page、Request、Session 和Application 范围的区别
可用于初学者学习jsp对象 容易掌握jsp的隐式对象
03-JSP隐式对象.ppt
JSP九大隐式对象学习,比较基础,适用入门人群
jsp的九大隐式对象详解
JSP 作用域与隐式对象[SP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1] 是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似...
TP4 JSP 隐式对象TP4
主要介绍了JSP中隐式对象用法,以实例形式分析了JSP隐式对象的定义及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
JavaWeb开发技术-JSP隐式对象.pptx
完整版Java JSP web开发教程 07_JSP隐式对象(共32页).ppt 完整版Java JSP web开发教程 08_JavaBean和JSP中(共26页).ppt 完整版Java JSP web开发教程 09_JSP表达式语言(共23页).ppt 完整版Java JSP web开发教程...
EL表达式,介绍EL表达的基本原理,详细的讲解了内置参数。
jsp+severlet笔记 jsp九大隐式对象 常见错误处理
JavaWeb程序设计入门课件JSP隐式对象共8页.pdf.zip
在JSP中,如果要将封装好的Cookie对象传送到客户端,可使用Response对象的addCookie()方法。 例如:response.addCookie(c)。 (4)读取保存到客户端的Cookie 使用Request对象的getCookie()方法,执行时将...
隐式对象 request 对象
JSP运行原理和九大隐式对象说明,需要的朋友可以参考一下
在JSP页面中,我们通过使用隐式对象可以更加方便地实现我们的效果。隐式对象包含: request response out ---- 输入输出对象 pageContext session application ---- 应用作用域对象 page config ---- Servlet对象...
第二部分就是Jsp技术,包括:jsp简介,jsp脚本和指令的使用,jsp隐式对象,JavaBean与jsp中的标准动作,jsp表达式语言,自定义标签,jsp自定义表签库. 第三部分就是过滤器的设计模式,包括过滤器和MVC设计模式. 目录 [隐藏] ...
JSP 经典之作 通讯录 其中包括 JSP 九大隐式对象的使用、 四大作用域的使用
JSP指令元素 JSP标准动作 隐式对象