码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-03-19 19:24:46      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

一、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

技术分享

运行结果:

技术分享

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!