瞭解JSP 1、什麼是JSP? JSP全名為Java Servlet pages,中文名為web伺服器頁面,它是在傳統的網頁HTML文件(*.htm,*.html)中插入java程式段和JSP標記,尾碼名為(*.jsp),其根本是一個簡化的Servlet設計。 2、為什麼要有JSP? 直接使用HTM ...
瞭解JSP
1、什麼是JSP?
JSP全名為Java Servlet pages,中文名為web伺服器頁面,它是在傳統的網頁HTML文件(*.htm,*.html)中插入java程式段和JSP標記,尾碼名為(*.jsp),其根本是一個簡化的Servlet設計。
2、為什麼要有JSP?
直接使用HTML文件(*.html)沒有辦法輸出java當中的信息,使用servlet輸出頁面非常麻煩,於是出現了jsp,既能寫HTML,又能寫java代碼。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 String name="今夕何希"; 12 %> 13 <h1><%=name %></h1> 14 15 </body> 16 </html>View Code
3、jsp的工作原理
(1)jsp本質是servlet
(2)jsp第一次訪問時會被web容器翻譯成servlet,在tomacat的work目錄下(E:\Program Files\Java\tomcat\apache-tomcat-7.0.90\work\Catalina\localhost\JSP\org\apache\jsp),能夠找到翻譯成的.java文件:
1 /* 2 * Generated by the Jasper component of Apache Tomcat 3 * Version: Apache Tomcat/7.0.90 4 * Generated at: 2018-09-15 13:13:30 UTC 5 * Note: The last modified time of this file was set to 6 * the last modified time of the source file after 7 * generation to assist with modification tracking. 8 */ 9 package org.apache.jsp; 10 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.servlet.jsp.*; 14 15 public final class Test_jsp extends org.apache.jasper.runtime.HttpJspBase 16 implements org.apache.jasper.runtime.JspSourceDependent { 17 18 private static final javax.servlet.jsp.JspFactory _jspxFactory = 19 javax.servlet.jsp.JspFactory.getDefaultFactory(); 20 21 private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; 22 23 private volatile javax.el.ExpressionFactory _el_expressionfactory; 24 private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; 25 26 public java.util.Map<java.lang.String,java.lang.Long> getDependants() { 27 return _jspx_dependants; 28 } 29 30 public javax.el.ExpressionFactory _jsp_getExpressionFactory() { 31 if (_el_expressionfactory == null) { 32 synchronized (this) { 33 if (_el_expressionfactory == null) { 34 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 35 } 36 } 37 } 38 return _el_expressionfactory; 39 } 40 41 public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { 42 if (_jsp_instancemanager == null) { 43 synchronized (this) { 44 if (_jsp_instancemanager == null) { 45 _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); 46 } 47 } 48 } 49 return _jsp_instancemanager; 50 } 51 52 public void _jspInit() { 53 } 54 55 public void _jspDestroy() { 56 } 57 58 public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) 59 throws java.io.IOException, javax.servlet.ServletException { 60 61 final javax.servlet.jsp.PageContext pageContext; 62 javax.servlet.http.HttpSession session = null; 63 final javax.servlet.ServletContext application; 64 final javax.servlet.ServletConfig config; 65 javax.servlet.jsp.JspWriter out = null; 66 final java.lang.Object page = this; 67 javax.servlet.jsp.JspWriter _jspx_out = null; 68 javax.servlet.jsp.PageContext _jspx_page_context = null; 69 70 71 try { 72 response.setContentType("text/html; charset=utf-8"); 73 pageContext = _jspxFactory.getPageContext(this, request, response, 74 null, true, 8192, true); 75 _jspx_page_context = pageContext; 76 application = pageContext.getServletContext(); 77 config = pageContext.getServletConfig(); 78 session = pageContext.getSession(); 79 out = pageContext.getOut(); 80 _jspx_out = out; 81 82 out.write("\r\n"); 83 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); 84 out.write("<html>\r\n"); 85 out.write("<head>\r\n"); 86 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n"); 87 out.write("<title>Insert title here</title>\r\n"); 88 out.write("</head>\r\n"); 89 out.write("<body>\r\n"); 90 out.write("\t"); 91 92 String name="今夕何希"; 93 94 out.write('\r'); 95 out.write('\n'); 96 out.write(' '); 97 out.print(name ); 98 out.write("\r\n"); 99 out.write("</body>\r\n"); 100 out.write("</html>"); 101 } catch (java.lang.Throwable t) { 102 if (!(t instanceof javax.servlet.jsp.SkipPageException)){ 103 out = _jspx_out; 104 if (out != null && out.getBufferSize() != 0) 105 try { 106 if (response.isCommitted()) { 107 out.flush(); 108 } else { 109 out.clearBuffer(); 110 } 111 } catch (java.io.IOException e) {} 112 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 113 else throw new ServletException(t); 114 } 115 } finally { 116 _jspxFactory.releasePageContext(_jspx_page_context); 117 } 118 } 119 }View Code
下一次訪問時,會看一下裡面的內容有沒有發生變化,如果變化了,重新編譯載入。
4、jsp組成部分
靜態數據,如HTML;JSP指令,如include指令;jsp標簽動作;用戶自定義標簽
- JSP腳本元素和變數
在jsp當中寫java代碼:
<%java代碼> 內部的java代碼翻譯到service方法的內部
<%=java變數或者表達式> 會被翻譯成service方法內部的out.print();
<%!java代碼 > 會被翻譯成selvlet的成員的內容
在jsp當中寫註釋:
HTML:<!-- 註釋內容--> 可見範圍 jsp源碼,翻譯後的servlet,頁面
java://單行註釋; /*多行註釋*/ 可見範圍 jsp源碼,翻譯後的servlet,頁面看不到
jsp: <%註釋內容%> 可見範圍 jsp源碼
- JSP指令
什麼是指令?jsp指令用於設置整個JSP頁面的相關信息,以及用於jsp頁面與其他容器間的通信。
有哪些指令?
page (1) 用於設定整個JSP頁面的屬性和相關功能
(2)page指令的主要屬性
(3)多個屬性間用空格隔開
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
include (1) 表示在JSP編譯時插入一個包含文件或代碼的文件
(2)include指令所包含的文件名不能是一個url,只能是靜態的文件名
(3)靜態包含
taglib (1)聲明jsp使用了哪些標簽庫
(2)JSP標準標簽庫,第三方標簽庫,自定義標簽庫
- 標簽動作 (1)頁麵包含(動態包含)
-
- (2)請求轉發:
<jsp:forward page="要轉發的資源"></jsp:forward>
- 隨式對象
jsp被翻譯成servlet之後,service方法中有9個對象定義並初始化完畢,開發人員可以直接調用它們而不用顯式地聲明它們再調用。
out對象:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 aaa 11 <%="bbb" %> 12 <% 13 out.write("ccc"); 14 response.getWriter().write("ddd"); 15 %> 16 17 </body> 18 </html>View Code
輸出為:
只有response能夠響應瀏覽器,因此out緩衝區(預設8kb)的內容會被拼接到response緩衝區之後。
pageContext對象:可以獲得其他8大隱式對象,可以向指定的其他域中存取數據