Java遇见HTML——JSP篇之JSP指令与动作元素

时间:2022-07-09 15:26:46

一、include指令(如:<%@include file="..."%> )

Java遇见HTML——JSP篇之JSP指令与动作元素

示例:

Date.jsp

 <%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Date d=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日");
String s=sf.format(d);
out.print(s);
%>

include.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>My JSP 'include.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>
<h1>include指令</h1>
<hr>
<%@include file="Date.jsp" %>
</body>
</html>

运行界面:访问include.jsp界面

Java遇见HTML——JSP篇之JSP指令与动作元素

二、include动作(如: <jsp:include page="..." flush="false">)

Java遇见HTML——JSP篇之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>My JSP 'include.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>
<h1>include动作</h1>
<hr>
<jsp:include page="Date.jsp" flush="false"></jsp:include>
</body>
</html>

运行结果:

Java遇见HTML——JSP篇之JSP指令与动作元素

三、include指令与include动作的区别

Java遇见HTML——JSP篇之JSP指令与动作元素

<jsp:include>动作在请求期间被执行,而include指令在编译期页面间被执行。

页面内容经常变化时更适合使用<jsp:include>动作。

页面内容不经常变化时更适合使用include指令

<jsp:include>动作包含的是执行结果,而include指令包含的是文件内容。

jsp:include这个其实就是:

Java遇见HTML——JSP篇之JSP指令与动作元素

include指令:

Java遇见HTML——JSP篇之JSP指令与动作元素

<%@ include %>编译后文件包括其所包含jsp的源代码;<jsp:include>编译后文件不包括,只写明所包含文件的名字,其和所包含文件之间是相对独立的存在。

四、forward动作

Java遇见HTML——JSP篇之JSP指令与动作元素

五、param动作

Java遇见HTML——JSP篇之JSP指令与动作元素

示例:

login.jsp

Java遇见HTML——JSP篇之JSP指令与动作元素

dologin.jsp

Java遇见HTML——JSP篇之JSP指令与动作元素

user.jsp

Java遇见HTML——JSP篇之JSP指令与动作元素

运行结果:

Java遇见HTML——JSP篇之JSP指令与动作元素