一、page指令
page指令是最常用的指令,用来说明JSP页面的属性等。JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里。但需要注意的是,无论在哪个page指令里的属性,任何page允许的属性都只能出现一次,否则会出现编译错误。import属性除外,可以出现多次。属性名称区分大小写。
属性名称 | 取值范围 | 描述 |
language | java | 指明解释该JSP文件时采用的语言。一般为Java语言。默认为Java。 |
extends | 任何类的全名 | 指明编译该JSP文件时继承哪个类。jsp为Servlet,因此当指明继承普通类时需要实现Servlet的init、destroy等方法。 |
import | 任何包名,类名 | 引入该JSP中用到的类、包等。import是唯一可以声明多次的page指令属性。一个import属性可以引用多个类,中间用英文逗号隔开,如<%@ page import="java.util.List,java.util.ArrayList"%>。 |
session | true,false | 指明该JSP内是否内置Session对象。如果为true,则内置Session对象,可直接使用。否则不内置Session对象。默认为true。 |
autoFlush | true,false | 是否运行缓存。如果为true,则使用out.println()等方法输出的字符串并不是立刻到达客户端服务器的,而是暂存在缓存里,缓存满或者程序执行完毕或者执行out.flush()操作时才到客户端。默认为true。 |
buffer | none或者数字+kb | 指定缓存大小。当autoFlush设为true时有效,例如:<%@ page buffer="10kb"%>。 |
isThreadSafe | true,false | 指定是否线程安全。如果为true,则运行多个线程同时运行该JSP程序,否则只运行一个线程运行,其余线程等待。默认为false。 |
isErrorPage | true,false | 指定该页面是否为错误处理页面,如果为true,则该JSP内置有一个Exception对象exception,可直接使用,否则没有,默认为fasle。 |
errorPage | 某个JSP页面的相对路径 | 指明一个错误显示页面,如果该JSP程序抛出了一个未捕捉的异常,则转到errorPage指定的页面。errorPage指定的页面通常isErrorPage属性为true,且内置的excception对象为未捕捉的异常。 |
contentType | 有效的文档类型 |
客户端浏览器根据该属性判断文档类型,例如: HTML格式为text/html 纯文本格式为text/plain JPG图像为image/jpeg GIF图像为image/gif WORD文档为application/msword |
info | 任意字符串 | 指明JSP的信息。该信息可以通过Servlet.getServletInfo()方法获取到。 |
trimDirectiveWhitespaces | true,false | 是否去掉指令前后的空白字符。默认为false。 |
trimDirectiveWhitespaces=“false”(默认为false)时HTML代码效果图:
trimDirectiveWhitespaces=“true”时HTML代码效果图:
注意:在HTML文件中,空行是不影响显示效果的。但如果输出的是XML文件,则可能有问题,因为某些XML解析器不允许XML文件前面有空行。
二、include指令
1.重点说明
include指令只有一种格式:<%@ include file="relativeURL"%>。relativeURL为本应用程序内另一个JSP文件或者HTML文件的路径。include指令用来实现JSP页面的的区块化。
2.代码实践和效果图
Head.jsp(导航栏内容)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>My JSP 'Head.jsp' starting page</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>
<table width="100%" cellspacing=1 bgcolor=#999999>
<tr>
<td bgcolor=#666666 colspan="7"
style="color:#FFFFFF; font-size:40px; height:60px;">
Hello World
</td>
</tr>
<tr>
<td bgcolor=#DDDDDD align="center">首页</td>
<td bgcolor=#DDDDDD align="center">百科</td>
<td bgcolor=#DDDDDD align="center">文档</td>
<td bgcolor=#DDDDDD align="center">下载</td>
<td bgcolor=#DDDDDD align="center">关于</td>
<td bgcolor=#DDDDDD align="center">邮件</td>
<td bgcolor=#DDDDDD align="center">社区</td>
</tr>
</table>
</body>
</html>
Foot.jsp(版权内容)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <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>
<table width="100%" cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td align="center" bgcolor=#666666 style="color: #FFFFFF;font-size:14px;height:20px;">
Copyright 2015-2016 ©King
</td>
</tr>
</table>
</body>
</html>
Include.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %><!-- 是否去掉指令前后的空白字符。默认为false -->
<%
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%>"> <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="Head.jsp" %> <!--include指令 -->
<p style="line-height:22px; text-indent:2em;">拉布拉多猎犬因原产地在加拿大的纽芬兰与拉布拉多省而得名。
拉布拉多犬是一种中大型犬类,个性忠诚、大气、憨厚、温和、阳光、开朗、活泼,智商极高,也对人很友善,
是非常适合被选作经常出入公共场合的导盲犬或地铁警犬及搜救犬和其他工作犬的狗品种,
跟哈士奇(西伯利亚雪撬犬)和金毛猎犬并列三大无攻击性犬类,拉布拉多智商位列世界犬类第七。</p>
<%@ include file="Foot.jsp" %> <!--include指令 -->
</body>
</html>
3.include行为和include指令
JSP还提供了另一种包含文件的行为(include行为):<jsp:include page="relativeURL">命令。该命令与include指令使用方法基本一致。不同的是include指令是把Head.jsp和Foot.jsp的源代码添加到Include.jsp中然后再编译成一个class文件,属于先包含后编译。而include行为则是运行时单独执行Head.jsp和Foot.jsp,然后把执行结果包含到Include.jsp中,属于先运行后包含行为。
除了上面两种方法包含文件外,还可以在web.xml中通过JSP配置来包含文件。
<jsp-config>
<jsp-property-group>
<include-prelude>/Head.jspf</include-prelude><!--在执行JSP之前执行的文件 -->
<include-coda>/Foot.jspf</include-coda><!--在执行JSP之后执行的文件 -->
</jsp-property-group>
</jsp-config>
三、taglib指令
JSP支持标签技术。使用标签功能能够实现视图代码重用。要使用标签功能必须先声明标签库以及标签前缀。taglib指令用来指明JSP页面内使用的JSP标签库。taglib指令有两个属性,uri为类库的地址,prifix为标签的前缀。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
</head>
<body>
<c:forEach var="item" items="${arrays}">
<c:out value="item"></c:out>
</c:forEach>
</body>
</body>
</html>