内置对象指的是服务器已经创建好的对象,可以直接使用
JSP一共有9个内置对象
request、response、out、page、pageContext、session、application、exception、config
jsp翻译成.java文件后,会在_jspService(request,response),直接在该方法体内直接使用,就是内置对象
<%@ 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> <% String path=request.getContextPath(); %> <%=path%> <% response.sendRedirect(path+"/b.jsp"); %> </body> </html> |
<%@ 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> b.jsp </body> </html> |
在JSP的_jspService方法中,有out,page对象,如下所示:
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
out = pageContext.getOut();
可见,out的类型是JspWriter,page即当前类(jsp翻译成servlet类)对象;
out可以用来输出内容到客户端,但是程序员一般不会使用,因为直接使用<%=%>即可以实现输出;
page也很少使用,与this相同
<%@ 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> <% JspWriter writer=pageContext.getOut(); Object p=pageContext.getPage(); pageContext.getException(); pageContext.getRequest(); pageContext.getResponse(); pageContext.getServletConfig(); pageContext.getServletContext(); pageContext.getSession(); %> </body> </html>
只能在当前页面有效,不能跨页面
<%@ 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("name", "zs"); %> <%=pageContext.getAttribute("name")%> </body> </html>
session是JSP中的另一个内置对象,是HttpSession类型的对象,可以在JSP中调用HttpSession接口中的任何方法;默认情况下,session内置对象存在;可以通过指令设置不存在:session="false",在页面上不能使用session内置对象了
<%@ 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> <% session.setAttribute("name", "lisi"); %> <%=session.getAttribute("name") %> </body> </html
是ServletContext类型的对象,可以在JSP中调用ServlletContext接口中的任何方法
<%@ 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> <% application.setAttribute("name", "wangwu"); %> <%=application.getAttribute("name") %> </body> </html>
_jspService()方法里会生成exception对象:
java.lang.Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);
if (exception != null) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>Title</title> <base href="<%=request.getContextPath()%>/"> </head> <body> <%=exception.getMessage()%> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="a.jsp" %> <html> <head> <title>Title</title> <base href="<%=request.getContextPath()%>/"> </head> <body> <%int i = 1 / 0;%> <%=i%> </body> </html>
<servlet> <servlet-name>testjsp</servlet-name> <jsp-file>/h.jsp</jsp-file> <init-param> <param-name>name</param-name> <param-value>zhaoliu</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>testjsp</servlet-name> <url-pattern>/testjsp</url-pattern> </servlet-mapping>
<%@ 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> <%=config.getInitParameter("name")%> </body> </html>
1.什么是内置对象?
服务器创建好的对象,直接使用的对象叫内置对象
2.JSP内置对象有哪些?
request 请求对象
response 响应对象
out 输出对象JspWriter out.write() out.print()
page jsp翻译成servlet类的对象 page=this
pageContext 页面作用域对象 不能跨页面,setAttribute(name,value) getAttribute(name) removeAttribute(name) 能得到其他8个内置对象
session HttpSession类型的对象 会话对象 setAttribute(name,value) getAttribute(name) removeAttribute(name) 默认情况下,session内置对象存在;可以通过指令设置不存在:session="false",在页面上不能使用session内置对象了
application ServletContext类型的上下文对象 setAttribute(name,value) getAttribute(name) removeAttribute(name)
config ServletConfig对象
1.web.xml配置jsp为servlet
<servlet>
<servlet-name>testjsp</servlet-name>
<jsp-file>/h.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value>zhaoliu</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>testjsp</servlet-name>
<url-pattern>/testjsp</url-pattern>
</servlet-mapping>
2.f.jsp
<%=config.getInitParameter("name")%>
exception 异常对象 默认不存在
1.设置指令 isErrorPage="true"
2.exception.getMessage()
3.引发异常页面
1.errorPage="g.jsp"
2.int i=1/0;
3.哪几个内置对象比较“特殊”?
1.其中pageContext对象封装了所有其他的内置对象;
2.session默认存在,可以通过指令配置为不存在 session="false";
3.exception默认不存在,当页面被指定为错误页面时才能使用isErrorPage="true"
import属性:用来引入JSP文件需要使用的类
<%@page import="java.util.*,java.io.*" %>
<%@page import="com.etc.vo.*" %>
上述代码可以在JSP文件中使用,引入JSP需要使用的Java类;
可以使用逗号同时引入多个包,也可以在一个JSP文件中多次使用import;
值得注意的是,import是page指令中唯一一个可以在一个JSP文件中多次出现的属性,其他属性在一个JSP文件中只能出现一次
<%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <base href="<%=request.getContextPath()%>/"> </head> <body> <%List<String> str = new ArrayList<>();%> </body> </html>
如下代码所示:
include指令是JSP中另外一个常用指令,用来静态包含其他页面;
所谓静态包含,指的是在翻译期间,把包含的页面也翻译到当前页面的Java文件中,也就是Java源文件即实现“二合一”
例如,在main.jsp中编写如下代码:
<%@include file="copyright.jsp"%>
过程:翻译main.jsp时,会把copyright.jsp文件翻译后插入到main.jsp翻译生成的.java文件中的相应位置;
<%@ 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> <%@include file="head.jsp" %> <h1>我是main内容</h1> </body> </html> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <h1>我是head内容</h1> |
![]() |
Taglib指令的语法:
<%@ taglib uri="uri" prefix="prefixOfTag" %>
uri属性确定标签库的位置,prefix属性指定标签库的前缀
JSP规范中定义了一系列的标准动作。Web容器按照规范进行了实现,可以解析并执行标准动作;
<jsp:action_name attribute1="value1" attribute2="value2">
</jsp:action_name>
其中action_name表示标准动作的名字,attribute1和attribute2是标准动作的若干个属性;
<jsp:include>是动态包含,即在运行期访问被包含的页面,并将响应结果同包含页面的响应结果合并,生成最终响应。
例如:
<jsp:include page=“top.jsp">
</jsp:include>
动态包含
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <h1>我是head内容</h1> |
<%@ 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> <jsp:include page="head.jsp"></jsp:include> <h1>我是main内容</h1> </body> </html> |
![]() |
include标准动作与include指令都是实现包含其他页面的功能;
include标准动作的属性是page,实现动态包含,发生在请求阶段;
include指令的属性是file,实现静态包含,发生在翻译阶段。
如下代码所示:
<jsp:forward page=“loginsuccess.jsp">
</jsp:forward>
上述代码将把请求转发到loginsuccess.jsp页面,类似在Servlet中调用RequestDispatcher的forward方法进行请求转发。
<%@ 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> <jsp:forward page="n.jsp"></jsp:forward> </body> </html> |
<%@ 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> n.jsp </body> </html> |
![]() |
<jsp:forward page="copyright.jsp">
<jsp:param name="author" value="etc"/>
</jsp:forward>
<jsp:include page="copyright.jsp">
<jsp:param name="author" value="etc"/>
</jsp:include>
上述代码使用param为forward和include动作传递参数,参数将被作为请求参数传递
<%@ 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> <jsp:forward page="n.jsp"> <jsp:param value="zs" name="name"/> </jsp:forward> </body> </html> |
<%@ 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> n.jsp<br/> <%=request.getParameter("name")%> </body> </html> |
![]() |
注意事项
使用标准动作时,一定注意正确结束标准动作,如<jsp:include>是标准动作的开始,一定要对应结束标记,如</jsp:include>。
如果标准动作没有动作体,就可以直接结束,如<jsp:include page="copyright.jsp"/>。
1.page指令有哪些常用属性?
import 引入java类 唯一一个可以多次使用的属性
pageEncoding 设置jsp页面的编码格式
errorPage 设置定位错误页面
isErrorPage 设置错误页面
session 会话可以使用
contentType 设置内容类型
2.include指令和include动作有什么区别?
include指令静态包含 翻译二合一(<%@ include file=”” %>)
include动作动态包含 翻译2个.java 请求运行动态包含进来(<jsp:include page=””></jsp:include>)
3.<jsp: forward page=”” ></forward> 请求转发 标签内不能写注释
<jsp: forward page=”” >
<jsp:param name=”” value=””></ jsp:param> 传递参数
</forward>