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

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

示例:

Date.jsp

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

include.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'include.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <h1>include指令</h1>
27     <hr>
28     <%@include file="Date.jsp" %> 
29   </body>
30 </html>

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

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

示例:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'include.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <h1>include动作</h1>
27     <hr>
28     <jsp:include page="Date.jsp" flush="false"></jsp:include>
29   </body>
30 </html>

运行结果:

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

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

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

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

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

jsp:include这个其实就是:

include指令:

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

四、forward动作

五、param动作

示例:

login.jsp

dologin.jsp

user.jsp

运行结果:

原文地址:https://www.cnblogs.com/Qian123/p/5295903.html