1.JSP标签简介
JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护。
2.JSP常用标签
- <jsp:include>标签
- <jsp:forward>标签
- <jsp:param>标签
2.1 <jsp:include>标签
<jsp:include>标签用于把另外一个资源的输出内容插入进当前jsp页面的输出内容之中,这种在jsp页面执行时的引入方式称之为动态引入。即jsp:include指令为动态包含。如果被包含的页面是JSP,则先处理之后再将结果包含,而如果包含的是非*.jsp文件,则只是把文件内容静态包含进来,功能与@include类似。
语法:<jsp:include page="relativeURL | <%=expression%>" flush="true|false" </jsp:include>
page属性用于指定被引入资源的相对路径,它可以通过执行一个表达式来获得;flush属性则指定在插入其他资源的输出内容时,是否将当前的jsp页面已经输出的内容刷新到客户端。
如下所示的includeTest3.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path3 = request.getContextPath();
String basePath3 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path3+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath3%>">
<title>My JSP 'includeTest3.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>
<h2>"includeTest3.jsp's content"</h2>
</body>
</html>
修改上篇中includeTest1.jsp代码,将@include指令改为<jsp:include page="includeTest3.jsp"></jsp:include>,运行结果同上,但是生成的新的includeTest1_jsp类的源码为:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.5.9
* Generated at: 2018-10-20 13:53:54 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp.jspTest;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class includeTest1_jsp exte