jsp

一、jsp腳本元素

標籤語法用途
聲明標籤<%!變量或方法聲明%>聲明jsp內所所用的全局變量或方法
表達式<%=%>輸出Java中的變量或者表達式的值到頁面上
程序代碼標籤<% 程序代碼 %>編寫Java程序代碼,但不可以定義方法


二、jsp頁面註釋


jsp註釋:<%-- 註釋 --%>,轉換階段消失(即在jsp轉換爲servlet階段),只能被開發人員看到。


Java註釋://、/*註釋*/、/**註釋*/,編譯階段消失(即jsp轉換成的servlet,然後轉換的servlet編譯成class文件的時候消失)。


HTML註釋:<!-- 註釋 -->,不會消失,在頁面中也能看到。


三、JSP指令

功能:

    用於指定jsp執行某些步驟。

    用於指示jsp表現特定的行爲。

語法格式:

    <%@ 指令 [屬性="屬性值"]%>

分類:

    page指令

    include指令

    tablib指令


3.1    page指令

    page屬性包含在"<%@"和"%>"之間。

    這些屬性可以單獨使用,也可以幾個或多個同時使用。

    page指令用來定義jsp文件的全局屬性。

    在jsp頁面中,只有import可以出現多次,其他屬性都只能出現一次。


3.2    include指令

    include指令的語法格式如下:

<%@ include file="filename" %>

    include指令的作用是在jsp頁面中靜態包含一個文件,同時由jsp解析包含的文件內容。

    靜態包含的含義:

        file不能爲一變量。

        不可以在file所指定的文件後接收任何參數。

    包含的是目標文件的源碼;包含過來,一起翻譯

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>頭部</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>
    頭部
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>菜單</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>
   菜單
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>腳本</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>
    腳本
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>主體</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>
    <%@ include file="/jsp/head.jsp" %>
    <%@ include file="/jsp/menu.jsp" %>
    主體
    <%@ include file="/jsp/foot.jsp" %>
  </body>
</html>


訪問之後,查看源代碼就可以看到。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>    
<head>    
<title>My JSP 'head.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">    
	-->    
<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>    
<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-7" data-genuitec-path="/day11/WebRoot/jsp/head.jsp">    
頭部    
</body>    
</html>    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>    
<head>    
<title>My JSP 'menu.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">    
	-->    
<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>    
<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-9" data-genuitec-path="/day11/WebRoot/jsp/menu.jsp">    
菜單    
</body>    
</html>    
主體    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>    
<head>    
<title>My JSP 'foot.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">    
	-->    
<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>    
<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-10" data-genuitec-path="/day11/WebRoot/jsp/foot.jsp">    
腳本    
</body>    
</html>    
</body>    
</html>

  這樣,就能體現上面的那句話了吧(包含的是目標文件的源碼;包含過來,一起翻譯)。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    頭部
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
   菜單
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 腳本
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>主體</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>
    <%@ include file="/jsp/head.jsp" %>
    <%@ include file="/jsp/menu.jsp" %>
    主體
    <%@ include file="/jsp/foot.jsp" %>
  </body>
</html>

訪問之後,查看源代碼就可以看到。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>    
<head>    
<title>主體</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">    
	-->    
<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>    
<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-8" data-genuitec-path="/day11/WebRoot/jsp/body.jsp">    
頭部    
菜單    
主體    
腳本    
</body>    
</html>

wKiom1jL07nhQlk4AABcGEURo-o384.png

3.3    taglib指令

    taglib指令用於在jsp頁面中導入標籤庫。

    常用屬性:

        uri        標籤文件的URL地址

        prefix   標籤組的命名空間前綴


四、jsp內置對象

內置對象代表內容範圍
request觸發服務應用的請求request
response 對請求的響應page
sesson爲請求的客戶創建的session對象session
application從servlet配置對象獲得servlet上下文

application

out向輸入流寫入內容的對象page
pageContext本jsp頁面的上下文page
page實現處理本也當前請求的類的實例page
config本jsp的ServletConfigpage
exception表示jsp頁面運行時產生的異常page


內置對象對應的類型

requestHttpServletRequest
responseHttpServletResponse
session

HttpSession

applicationServletContext
configServletConfig
pageObject
pageContextPageContext
exceptionThrowable
outJspWriter


五、jsp的四種數據範圍

對象範圍
pageContextpage範圍
request請求範圍
session會話範圍
application應用範圍


六、jsp常用標籤

<jsp:include>文件包含 動態包含
<jsp:forward>在jsp頁面完成轉發
<jsp:param> 在請求和轉發之前攜帶參數


七、靜態包含和動態包含的區別?

wKioL1jL14SDgeceAAHgJz7ePeY663.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章