jsp(2)
目录
1.Jsp指令
JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分。
1.1 include指令
include指令用于引入其它JSP页面,如果使用include指令引入了其它JSP页面,那么JSP引擎将把这两个JSP翻译成一个servlet。所以include指令引入通常也称之为静态引入。 语法: <%@ include file="header.jsp" %>
注:1)原理就是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp)中,合并翻译成一个java源文件,再编译运行!
- 如果使用静态引入,被包含页面中不需要出现全局的html标签了!(如html、head、body)。
- file属性用于指定被引入文件的相对路径。 file属性的设置值必须使用相对路径,如果以“/”开头,表示相对于当前WEB应用程序的根目录(注意不是站点根目录),否则,表示相对于当前文件。
例:在header.jsp文件中,写入:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%-- 被包含页面不需要出现全局的HTML标签,如:HTML、head、body等--%>
<h1>这是网页的头部</h1>
在index.jsp文件中,写入:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>jsp指令之 include</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%--导入 --%>
<%-- <%@include file="header.jsp" %> --%>
<%@include file="/header.jsp" %><!-- web应用程序的根目录,即webRoot -->
<h1>这是网页的主体</h1>
</body>
</html>
浏览器查看:
JSP引擎将把这两个JSP翻译成一个servlet,具体可以查看,index_jsp.java源文件
可以看到,header.jsp中的内容,被写到了index.jsp中的)_jspService方法中,即合并在了一起,通过out.write()方法写出到浏览器。
1.2 page指令
page指令用于定义JSP页面的各种属性,无论page指令出现在JSP页面中的什么地方,它作用的都是整个JSP页面,page指令最好是放在整个JSP页面的起始位置。
<%@ page
language="java"; 告诉服务器使用什么动态语言来翻译jsp文件。
import="java.util.* , java.io.*; 告诉服务器java文件导入的包,多个包之间用逗号。
session="true | false" 是否开启session功能。false,不能用session隐式对 象;true,可以使用session隐式对象。
buffer="none | 8kb | sizekb" jsp页面的缓存区大小。
autoFlush="true | false"
isThreadSafe="true | false"
info="text"
errorPage="relative_url" 指定当前jsp页面的错误处理页面。
isErrorPage="true | false" 指定当前页面是否为错误处理页面。false,不是错误处理 页面,不能使用exception隐式对象;true,是错误处理 页面,可以使用exception隐式对象。
pageEncoding="ISO-8859-1" 告诉服务器用什么编码翻译.jsp到.java文件。
contentType="text/html ; charset=ISO-8859-1" 服务器发送浏览器的数据类型和内容编码
注:在开发工具中,只需要设置pageEncoding即可解决中文乱码问题。
isELIgnored="true | false" 是否忽略EL表达式。
%>
注:①对import="java.util.**; JSP引擎自动导入的包:java.lang.*、javax.servlet.*、javax.servlet.jsp.*、javax.servlet.http.*
②errorPage属性的设置值必须使用相对路径,如果以“/”开头,表示相对于当前WEB应用程序的根目录(注意不是站点根目录),否则,表示相对于当前页面。
可以在web.xml文件中为整个WEB应用程序设置错误处理页面。如果设置了某个JSP页面的errorPage属性,那么在web.xml文件中设置的错误处理将不对该页面起作用。即符合就近原则。
配置全局的错误处理页面:
<!-- 全局错误处理页面配置 -->
<web-app>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/404.html</location>
</error-page>
</web-app>
例:
<%@ page language="java"
import="java.util.*"
pageEncoding="utf-8"
session="true"
errorPage="error.jsp"
%>
<%-- jsp之 page指令--%>
<html>
<head>
<title>jsp之page指令</title>
</head>
<body>
session id:<%=session.getId() %>
<% String name=null;
%>
<% name.charAt(1); %>
</body>
</html>
<!--this file is error.jsp -->
<%@ page language="java"
import="java.util.*"
pageEncoding="utf-8"
session="true"
errorPage="error.jsp"
%>
<%-- jsp之 page指令--%>
<html>
<head>
<title>页面出错了</title>
</head>
<body>
不好意思,主页君掉线了
</body>
</html>
1.3 taglib指令
用于在JSP页面中导入标签库
2. Jsp的隐式对象
Sun公司设计Jsp时,在jsp页面加载完毕之后就会自动帮开发者创建好这些对象,而开发者只需要直接使用这些对象调用方法即可。
隐式对象名 类型
pageContext PageContext
request HttpServletRequest
session HttpSession
application ServletContext
response HttpServletResponse
config ServletConfig
exception Throwable
page Object(this)
out JspWriter
2.1 out对象
out对象类型是JspWriter类,相当于带缓存的PrintWriter。
注:①设置JSP页面的page指令的buffer属性可以调整它的缓存大小,甚至关闭它的缓存。
②只有向out对象中写入了内容,且满足如下任何一个条件时,out对象才去调用ServletResponse.getWriter方法,并通过该方法返回的PrintWriter对象将out对象的缓冲区中的内容真正写入到Servlet引擎提供的缓冲区中:1)设置page指令的buffer属性关闭了out对象的缓存功能2)out对象的缓冲区已满3)整个JSP页面结束4)刷新缓冲区。
out隐式对象的工作原理图:
2.2 pageContext对象
pageContext对象的类型是PageContext,它叫jsp的上下文对象。
1)可以获取其他八个内置对象
HttpServletRequest request=pageContext.getRequest();
HttpSession session=pageContext.getSession();
ServletContext application=pageContext.getServletContext();
HttpServletResponse response=pageContext.getResponse();
ServletConfig config=pageContext.getServletConfig();
Throwable exception=pageContext.getException();
Object page=pageContext.getPage();
JspWriter out=pageContext.getOut();
使用场景: 在自定义标签的时候,PageContext对象频繁使用到!
2)本身是一个域对象。
pageContext PageContext page域:只能在当前jsp页面中使用(当前页面)
request HttpServletRequest request域:只能在同一个请求中使用(转发)
session HttpSession session域:只能在同一个会话(session对象)中使用
application ServletContext application域:只能在同一个web应用中使用。
即jsp中有四个域对象。servlet中只有后三个域对象。
①pageContext对象的方法 (默认在page域)
public void setAttribute(java.lang.String name, java.lang.Object value)
public java.lang.Object getAttribute(java.lang.String name)
public void removeAttribute(java.lang.String name)
②pageContext对象中还封装了访问其它域的方法
public void setAttribute(java.lang.String name, java.lang.Object value, int scope)
public java.lang.Object getAttribute(java.lang.String name, int scope)
public void removeAttribute(java.lang.String name, int scope)
其中,代表各个域的常量(scope):
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext..SESSION_SCOPE
PageContext.APPLICATION_SCOPE
③自动在四个域中搜索数据
pageContext.findAttribute(String name);
顺序: page域 -> request域 -> session域- >application域
3)引入和跳转到其他资源
pageContext.forward(String relativeUrlPath);
pageContext.include(String relativeUrlPath);
pageContext.include(String relativeUrlPath, boolean flush);
分别简化和替代RequestDispatcher.forward()方法和include()方法。传递给这些方法的资源路径,如果路径以“/”开头,表示相对于当前WEB应用程序的根目录,否则,表示相对于当前JSP所映射到的访问路径。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>pageContext对象</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//1.获取其他八个内置隐式对象
response.getWriter().write("同一个对象? "+(out==pageContext.getOut()));
%>
<hr/>
<%
//2.作为域对象
pageContext.setAttribute("name", "xiaohua");//默认保存在page域
pageContext.setAttribute("name","xiaohua in request",PageContext.REQUEST_SCOPE);//指定保存到request域
//等价于:request.setAttribute("name","xiaohua in request");
//获取数据
String name=(String) pageContext.getAttribute("name");//默认获取page域
String name2=(String)pageContext.getAttribute("name",PageContext.REQUEST_SCOPE);
out.write(name2);
%>
<hr/>
<%--自动搜索 顺序:page域->request域->session域->application域 --%>
<%= pageContext.findAttribute("name") %>
</body>
</html>
浏览器,查看: