一、JSP指令

(1)page指令

指令格式如下:

<%@page 属性名1="属性值1" 属性名="属性值2" ... %>

比如下面这样:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.awt.*" %>
<%@ page import="java.util.*","java.awt.*" %>

(2)include指令

需要再JSP页面中包含领一个JSP页面,一般使用include指令

格式如下:

<%@ include file="被包含的文件地址"%>

右击Web文件夹,New—>JSP/JSPX,名称为date,代码参考如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<html>
<head><title>date</title>
</head>
<body>
当前时间是:
<%
    Date date = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String today =df.format(date);
    out.println(today);

</body>
</html>

继续右击Web文件夹,New—>JSP/JSPX,名称为include,代码参考如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
    <title>欢迎你</title>
</head>
<body>
<%@ include file="date.jsp"%>
</body>
</html>

运行一下tomcat,在浏览器中访问下面的地址:

http://localhost:8080/chapter06/include.jsp

(3)taglib指令

taglib指令标识该页面中所使用的标签库。基本格式如下:

<%@ taglib prefix="tagPrefix" uri="URL" %>
二、JSP动作元素

(1)包含文件元素<jsp:include>

<jsp:include>动作元素用于向当前页引入其他的文件。

右击Web文件夹,New—>JSP/JSPX,名称为included,代码参考如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>include</title>
</head>
<body>
<%Thread.sleep(5000);%>
included.jsp内的中文<br />
</body>
</html>

继续右击Web文件夹,New—>JSP/JSPX,名称为dynamiclnclude,代码参考如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>dynamicInclude page</title>
</head>
<body>
dynamicInclude.jsp内的中文
<br />
<jsp:include page="included.jsp" flush="true" />
</body>
</html>

在浏览器中输入

http://localhost:8080/chapter06/dynamiclnclude.jsp

运行后效果如下:

(2)包含文件元素<jsp:forward>

forward动作元素可以将当前请求转发到其他Web资源,格式如下:

<jsp:forward page="relativeURL" />

右击Web文件夹,New—>JSP/JSPX,名称为jspforward,代码参考如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>forword page</title>
</head>
<body>
<jsp:forward page="welcome.jsp" />
</body>
</html>

继续右击Web文件夹,New—>JSP/JSPX,名称为welcome,代码参考如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>welcome page</title>
</head>
<body>
你好,欢迎进入首页,当前访问时间是:
<%
    out.print(new java.util.Date());

</body>
</html>

在浏览器中输入下面的地址:

http://localhost:8080/chapter06/jspforward.jsp

效果如下: