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

目录

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

二、include动作(如: )

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

四、forward动作

五、param动作


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

示例:

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界面

 

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

示例:

<%@ 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>

运行结果:

 

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

运行结果:

猜你喜欢

转载自blog.csdn.net/guifei0/article/details/83210469
今日推荐