# 1/16 JSP 觀念 ###### tags: `JSP` # 5-2 大綱 Predefine Variables 事先宣告變數名稱 # 5-3 JSP 可套用HTML&Java 開發者不須編譯,底層執行方式是servlet 靜態樣板html 動態內容jsp # 5-4 JSP特性 元素 scipting(elements,directives,actions) tag標記 Template=樣板文字 # 5-5 JSP翻譯與執行步驟 translation(翻譯) = (jsp)翻譯(java)實作jsp程式碼翻譯java程式碼->class execution(編譯)=變成class黨 execution後才能跟客戶端服務 ![](https://i.imgur.com/j7PPCSk.png) jsp fragment(片段) .jspf(片檔名) # 9 實作 http://localhost:8080/JSPDemo/HelloUserServlet(?userName=aaa) # 5-12.13.14 JSP的生命週期(Life Cycle) 用過jsp之後再次呼叫jsp已被servlet的記憶體紀錄 所以不會在編譯 list.jsp轉譯->list_jsp.java編譯->list_jsp.class載入->servle(實例化) void _jspservice=doget,dopost ![](https://i.imgur.com/kr5RYsb.png) ![](https://i.imgur.com/t8P4mNt.png) <%= xx.xx%>動態語法 # 5-19 JSP語法 Scripting(Declaration(宣告),Scriplet(程式敘述),Expression(運算式)) Directive elements(指示)(page網頁,include,taglib) Action=JavaBean(模型) Expression Language(EL)簡短程式 Custom Tag(客制化標籤)5-177頁 Comment(註解) # 5-20 六大類jsp語法元素 Directive指示用在translation(翻譯)<%@ %> Action(動作):執行階段<jsp:actionName /> Declaration(宣告)<%! %> Scriptlet(程式碼)<% %> Expression(運算式)<%= %> Comment(註解)<%-- --%> ![](https://i.imgur.com/sOQzuNs.png) |二二|=方法 # 5-21 JSP語法例 <!-- -->HTML <% /* */%>Java <%=運算式%>運算式不要寫分號; # 5-22.23.24.25 宣告元素(Declaration) 宣告定義變數與方法 ![](https://i.imgur.com/dD2ReXf.png) string name this.name=自己的欄位name ![](https://i.imgur.com/8gVOM4n.png) 轉譯後 ![](https://i.imgur.com/eKQHtlE.png) 重新定義 jspInit(),jspDestroy() ![](https://i.imgur.com/SBAxgAU.png) servlet對應JspPage對應httpjsppage httpservlet對應httpjsppage ![](https://i.imgur.com/OHSuYHo.png) <%! %>可宣告變數跟方法 <% %>(編譯的地方再方法裡面)不能宣告方法 (area.jsp) ![](https://i.imgur.com/VwPTFRy.png) (iterationTest.jsp) ![](https://i.imgur.com/0an8nlV.png) ![](https://i.imgur.com/78N7IrB.png) # 5-26 JSP-Scriptlets Scriptlets是嵌入(embedded)的java code <%count++;%> # 5-27 JSP-Expression <%=count%>or<%=++count%> Expression不需要寫分號; Expression可以是Java Method的呼叫 (initTest.jsp) ![](https://i.imgur.com/ZcLADAU.png) ![](https://i.imgur.com/bvm9cqL.png) # 5-28 JSP範例-visitorcounter.jsp ![](https://i.imgur.com/vQwT4fT.png) ![](https://i.imgur.com/VksE8x1.png) # 29 DayWish.jsp ![](https://i.imgur.com/KyWi1Dm.png) Math.random<0.5 =小於亂數之間 ![](https://i.imgur.com/auOeXGw.png) # 30 ApplesAndOranges.jsp ``` <% String format = request.getParameter("format"); if ((format != null) && (format.equals("excel"))) { response.setContentType("application/vnd.ms-excel");//多媒體格式 } %> ``` http://localhost:8080/JSPDemo/ApplesAndOranges.jsp(?format=excel)參數格式 https://zh.wikipedia.org/wiki/%E5%A4%9A%E7%94%A8%E9%80%94%E4%BA%92%E8%81%AF%E7%B6%B2%E9%83%B5%E4%BB%B6%E6%93%B4%E5%B1%95 # 5-31.32.33 Action execution執行階段 用在request processing phase flush="true"(立刻送出) jsp:include就是Action的一種 ![](https://i.imgur.com/OdP9QZc.png) jsp:plugin外掛 (copyright.jsp) ![](https://i.imgur.com/kMUrkLd.png) # 5-35.36 JSP的Directives 還沒到request階段 ![](https://i.imgur.com/sGqmEbQ.png) ![](https://i.imgur.com/fXCqWgq.png) page include taglib(公用標籤 特殊語法 自首開頭代表哪個標籤的定義 taglib.tld定義哪個標籤) ![](https://i.imgur.com/6p1XOzl.png) # 5-37 Jsp Directives ![](https://i.imgur.com/shQe8X8.png) # 5-38.39 指示元素-page ![](https://i.imgur.com/x8uE7A4.png) ![](https://i.imgur.com/JJ4Z4R1.png) 每個屬性標籤中間用空白隔開 # 5-40 jsp 預設啟動session extends不要去啟動 內建有 buffer =(kilobytes) ![](https://i.imgur.com/3KXW590.png) # 5-41 指示元素-page ![](https://i.imgur.com/jI7n9Tp.png) vnd廠商 ![](https://i.imgur.com/CaFLvxa.png) # 5-42 指示元素-include ![](https://i.imgur.com/6tVtOPw.png) ``` <%@ include file="copyright.jspf" %> <jsp:include page="copyright2.jsp" flush="ture" /> ``` ![](https://i.imgur.com/KE6FKl5.png) file會合併 Action會各自產生 ![](https://i.imgur.com/JuOmQee.png) ![](https://i.imgur.com/5bmB317.png) # 5-45 指示元素的統一設定 ![](https://i.imgur.com/ugHfzU2.png) *.jsp副檔名做pattern utf-8 16kb buffer ``<include-pattern>檔頭</include-pattern>`` ``<include-coda>檔尾</include-coda>`` ![](https://i.imgur.com/L2PUAST.png) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ``` 白皮書 # 5-47 Scriptlet元素 只要是Scriptlet的東西都會放到_jspService() ![](https://i.imgur.com/guJUkY7.png) # 5-48.49 if敘述 迴圈範例 ![](https://i.imgur.com/ZpKJxqM.png) ![](https://i.imgur.com/DetVCf3.png) (conditionalTest1.jsp),(conditionalTest2.jsp) 1![](https://i.imgur.com/yCiAkM7.png) 2![](https://i.imgur.com/nxW8buz.png) # 5-50 禁用Scriptlet ![](https://i.imgur.com/3DJ0oqE.png) (initializationOfVariables.jsp) ![](https://i.imgur.com/V0ihvGx.png) <%!%>->宣告放哪都不影響 <%%>會有順序問題 一定要宣告才能使用 (orderOfScriptlets.jsp) ![](https://i.imgur.com/q1EHYYE.png) # 5-51 運算式(Expression)元素 Expression=<%= %> ![](https://i.imgur.com/U2nXMjk.png) # 5-52.53 註解(Comment)元素 ![](https://i.imgur.com/CkZyKMj.png) jsp(註解)->java(註解)->class(註解)->brower(註解) ![](https://i.imgur.com/K4ao1FN.png) (welcome.jsp)![](https://i.imgur.com/pjc2bSk.png) # 5-54 引號與脫逸號(Escape)用法 ![](https://i.imgur.com/BfjUIXE.png) (escapeSequence.jsp)![](https://i.imgur.com/DZx2aNt.png) # 5-55 JSP錯誤狀況與處裡 JSP轉換為Servlet原始碼時 Servlet原始碼進行編譯時 Servlet載入容器進行服務但發生執行時期錯誤時 # 5-56.57 JSP轉換為Servle原始碼時 ![](https://i.imgur.com/ubMR4Ab.png) `` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" buffer="dsad8kb" %> `` ![](https://i.imgur.com/EnpOToW.png) # 5-58.59 進行服務但發生執行時期錯誤時 ![](https://i.imgur.com/eDPItgv.png) ![](https://i.imgur.com/MK29uTo.png)