内置对象
1、定义
- JSP内置对象是Web容器创建的一组对象,不使用new关键字创建,就可以直接使用。
- 在xxx_jsp.java 文件中对内置对象做了声明,由容器实现和管理,在所有的Jsp页面中都能使用内置对象,内置对象在JSP初始化时生成。
JSP的9个标准内置对象
1、application
- 是 ServletContext 类型
2、request
- 是HttpServletRequest类型
3、response
- 是HttpServletResponse类型
4、page
- 和this是同一个对象
- 是一个servlet
5、session
- 是HttpSession类型
6、config
- 是ServletConfig类型
- 在web.xml中配置初始化参数(使用jsp-file标签制定特定的jsp页面):
<servlet> <servlet-name>JspConfig</servlet-name> <jsp-file>指定要使用config的jsp的页面</jsp-file> <init-param> <param-name>name</param-name> <param-value>Edward</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JspConfig</servlet-name> <url-pattern>/config.do</url-pattern> </servlet-mapping>
测试案例:
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>config</title>
</head>
<body>
<h3>
InitParameter : <%=config.getInitParameter( "name") %>
</h3>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>config.do</servlet-name>
<jsp-file>/builtin/config.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value>罗玉凤</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>config.do</servlet-name>
<url-pattern>/config.do</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>config.do</servlet-name>
<jsp-file>/builtin/config.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value>罗玉凤</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>config.do</servlet-name>
<url-pattern>/config.do</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP的9个标准内置对象</title>
</head>
<body>
<h3>JSP的9个标准内置对象</h3>
<ul>
<li><a href="">application</a>
<ol>
<li> <%out.println(application); %></li>
<li> <%= application %></li>
<li> application 是否是 ServletContext 类型: <%=application instanceof ServletContext%></li>
</ol>
</li>
<li><a href="">request</a>
<ol>
<li> <%=request %></li>
<li> request 是否是 HttpServletRequest 类型: <%=request instanceof HttpServletRequest%></li>
</ol>
</li>
<li><a href="">response</a>
<ol>
<li> <%= response %> </li>
<li> response 是否是 HttpServletResponse 类型: <%= response instanceof HttpServletResponse %> </li>
</ol>
</li>
<li><a href="">session</a>
<ol>
<li> <%=session %> </li>
<li> session 是否是 HttpSession 类型: <%= session instanceof HttpSession %> </li>
</ol>
</li>
<li><a href="">pageContext</a>
<ol>
<li> <%= pageContext %> </li>
<li> pageContext 是否是 PageContext 类型: <%= pageContext instanceof PageContext %></li>
<li><a href="<%= request.getContextPath()%> /builtin/pageContext.jsp">pageContext</a></li>
</ol>
</li>
<li><a href="">out</a>
<ol>
<li> <%=out %> </li>
<li> out 是否是 JspWriter 类型: <%=out instanceof JspWriter %></li>
</ol>
</li>
<li><a href="javascript:;">config</a>
<ol>
<li> <%= config %> </li>
<li> config 是否是 ServletConfig 类型: config instanceof ServletConfig %> </li>
<li><a href="<%= request.getContextPath()%> /builtin/config.jsp">config ( jsp )</a></li>
<li><a href="<%= request.getContextPath()%>/config.do">config ( servlet ) </a></li>
</ol>
</li>
<li><a href="">exception</a>
<ol>
<li> <a href="<%= request.getContextPath() %>/throw.jsp">throw</a> </li>
</ol>
</li>
<li><a href="">page</a>
<ol>
<li> <%= page %> </li>
<li> page 和 this 是否是 同一个对象: <%=page == this %> </li>
<li> page 是否是 一个 Servlet : <%=page instanceof Servlet %> </li>
</ol>
</li>
</ul>
</body>
</html>
运行结果如下:
JSP页面配置其实也是在web.xml文件中的进行,JSP被当成Servlet配置,为Servlet配置参数使用init-param元素,config内置对象就可以接受param-name和param-value分别指定参数名和参数值。
7、out
- 是JspWriter类型
- JspWriter直接继承了Writer
8、exception
- 对于要处理异常的页面,需要声明这是一个错误指令页面
<%@ page isErrorPage = "true" %>
- 在可能发生异常的页面,需要制定错误处理页面
<%@ page errorPage= "catch.jsp"%>
- 针对特定的错误页面处理:
在web.xml中配置(针对特定错误代码或者异常类型)
<error-page>
<error-code>错误代码</error-code>
<exception-type>异常类型</exception-type>
<location>发生错误去的页面</location>
</error-page>
测试案例:
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8" %>
<%@ page errorPage= "catch.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>throw</title>
</head>
<body>
<h3>在可能发生异常的页面中使用 <%@ page errorPage="catch.jsp" %> 指定错误处理页面 </h3>
<h3>
<%=100 / 0 %>
</h3>
</body>
</html>
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8" %>
<%@ page isErrorPage = "true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>exception</title>
</head>
<body>
<h3>对于要负责处理异常的页面,首先需要通过指令声明这是一个错误处理页面</h3>
<h3>只有声明了 <%@ page isErrorPage="true" %> 的页面才会有 exception 对象存在</h3>
<h3> exception : <%= exception %></h3>
<h3> exception message : <%=exception.getMessage()%></h3>
</body>
</html>
运行结果如下:
内部发生的是forward操作,浏览器地址没有发生改变
404页面处理测试案例:
<error-page>
<error-code>404</error-code>
<location>/builtin/404.jsp</location>
</error-page>·
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
<h1>404</h1>
<h3>你所访问的页面被外星人劫持了</h3>
</body>
</html>
9、pageContext
- 是PageContext类型(PageContext是JspContext的子类)
- 可以通过pageContext获得其他内置对象
测试案例:
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageContext</title>
</head>
<body>
<h4>
application:<%=pageContext.getServletContext()%>
</h4>
<h4>
request:<%=pageContext.getRequest()%>
</h4>
<h4>
response:<%=pageContext.getResponse() %>
</h4>
<h4>
session:<%=pageContext.getSession()%>
</h4>
<h4>
config:<%=pageContext.getServletConfig() %>
</h4>
<h4>
page:<%=pageContext.getPage()%>
</h4>
<h4>
out:<%=pageContext.getOut()%>
</h4>
<h4>
exception:<%=pageContext.getException()%>
</h4>
<hr>
<h4>
Eexpresssion Language:<%=pageContext.getExpressionEvaluator()%>
</h4>
</body>
</html>
转载请于明显处标明出处