1、jsp腳本元素javascript
標籤 | 語法 | 用途 |
聲明標籤 | <%!變量或方法聲明%> | 聲明jsp內所所用的全局變量或方法 |
表達式 | <%=%> | 輸出Java中的變量或者表達式的值到頁面上 |
程序代碼標籤 | <% 程序代碼 %> | 編寫Java程序代碼,但不能夠定義方法 |
2、jsp頁面註釋css
jsp註釋:<%-- 註釋 --%>,轉換階段消失(即在jsp轉換爲servlet階段),只能被開發人員看到。html
Java註釋://、/*註釋*/、/**註釋*/,編譯階段消失(即jsp轉換成的servlet,而後轉換的servlet編譯成class文件的時候消失)。java
HTML註釋:<!-- 註釋 -->,不會消失,在頁面中也能看到。session
3、JSP指令
app
功能:jsp
用於指定jsp執行某些步驟。
async
用於指示jsp表現特定的行爲。
ide
語法格式:ui
<%@ 指令 [屬性="屬性值"]%>
分類:
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>
3.3 taglib指令
taglib指令用於在jsp頁面中導入標籤庫。
經常使用屬性:
uri 標籤文件的URL地址
prefix 標籤組的命名空間前綴
4、jsp內置對象
內置對象 | 表明內容 | 範圍 |
request | 觸發服務應用的請求 | request |
response | 對請求的響應 | page |
sesson | 爲請求的客戶建立的session對象 | session |
application | 從servlet配置對象得到servlet上下文 | application |
out | 向輸入流寫入內容的對象 | page |
pageContext | 本jsp頁面的上下文 | page |
page | 實現處理本也當前請求的類的實例 | page |
config | 本jsp的ServletConfig | page |
exception | 表示jsp頁面運行時產生的異常 | page |
內置對象對應的類型
request | HttpServletRequest |
response | HttpServletResponse |
session | HttpSession |
application | ServletContext |
config | ServletConfig |
page | Object |
pageContext | PageContext |
exception | Throwable |
out | JspWriter |
5、jsp的四種數據範圍
對象 | 範圍 |
pageContext | page範圍 |
request | 請求範圍 |
session | 會話範圍 |
application | 應用範圍 |
6、jsp經常使用標籤
<jsp:include>文件包含 動態包含
<jsp:forward>在jsp頁面完成轉發
<jsp:param> 在請求和轉發以前攜帶參數
7、靜態包含和動態包含的區別?