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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值