當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
[02] JSP内置对象
生活随笔
收集整理的這篇文章主要介紹了
[02] JSP内置对象
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、內(nèi)置對象的來歷
JSP是由一些內(nèi)置對象的,即不需要定義,也不需要我們主動創(chuàng)建,就可以直接使用的對象。當(dāng)然,其對象名稱也是固定的,無法修改,我們可以直接調(diào)用其相關(guān)方法。在 [01] JSP的基本認識 已經(jīng)說過JSP的本質(zhì),并明白了其運行的流程,容器會幫我們將JSP翻譯成為Java類,其中會有一些“固定”代碼,我們還是先看其核心方法:public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {PageContext pageContext = null;HttpSession session = null;ServletContext application = null;ServletConfig config = null;JspWriter out = null;Object page = this;JspWriter _jspx_out = null;PageContext _jspx_page_context = null;try {response.setContentType("text/html");pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);_jspx_page_context = pageContext;application = pageContext.getServletContext();config = pageContext.getServletConfig();session = pageContext.getSession();out = pageContext.getOut();_jspx_out = out;//我們自定義的Java代碼會被翻譯到這個位置} catch (Throwable t) {if (!(t instanceof SkipPageException)) {out = _jspx_out;if (out != null && out.getBufferSize() != 0)try {out.clearBuffer();} catch (java.io.IOException e) {}if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);}} finally {_jspxFactory.releasePageContext(_jspx_page_context);} }x1public void _jspService(HttpServletRequest request, HttpServletResponse response) 2 ? ? ? ?throws java.io.IOException, ServletException {34 ? ?PageContext pageContext = null;5 ? ?HttpSession session = null;6 ? ?ServletContext application = null;7 ? ?ServletConfig config = null;8 ? ?JspWriter out = null;9 ? ?Object page = this;10 ? ?JspWriter _jspx_out = null;11 ? ?PageContext _jspx_page_context = null;1213 ? ?try {14 ? ? ? ?response.setContentType("text/html");15 ? ? ? ?pageContext = _jspxFactory.getPageContext(this, request, response,16 ? ? ? ? ? ? ? ?null, true, 8192, true);17 ? ? ? ?_jspx_page_context = pageContext;18 ? ? ? ?application = pageContext.getServletContext();19 ? ? ? ?config = pageContext.getServletConfig();20 ? ? ? ?session = pageContext.getSession();21 ? ? ? ?out = pageContext.getOut();22 ? ? ? ?_jspx_out = out;23 24 ? ? ? ?//我們自定義的Java代碼會被翻譯到這個位置2526 ? } catch (Throwable t) {27 ? ? ? ?if (!(t instanceof SkipPageException)) {28 ? ? ? ? ? ?out = _jspx_out;29 ? ? ? ? ? ?if (out != null && out.getBufferSize() != 0)30 ? ? ? ? ? ? ? ?try {31 ? ? ? ? ? ? ? ? ? ?out.clearBuffer();32 ? ? ? ? ? ? ? } catch (java.io.IOException e) {33 ? ? ? ? ? ? ? }34 ? ? ? ? ? ?if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);35 ? ? ? }36 ? } finally {37 ? ? ? ?_jspxFactory.releasePageContext(_jspx_page_context);38 ? }39}
可以看到,在方法的開頭中定義了一系列變量,在 try catch 塊中對變量進行了賦值。根據(jù)容器翻譯JSP文件的流程我們知道,我們自定義的Java代碼都是加載在“固定”代碼之后,也就是變量賦值之后,所以我們完全可以使用該方法體中內(nèi)部定義的變量,以及方法的參數(shù),并且可以順利執(zhí)行。
于是我們常用的JSP內(nèi)置對象就這么有了:
| 類型? ?? | 變量名? ?? | 備注 |
| HttpServletRequest? ?? | request? ?? | |
| HttpServletResponse? ?? | response? ?? | |
| PageContext? ?? | pageContext? ?? | JSP上下文對象,可以由此獲取其他內(nèi)置對象 |
| HttpSession? ?? | session? ?? | |
| ServletContext? ?? | application? ?? | |
| ServletConfig? ?? | config? ?? | |
| JspWriter? ?? | out? ?? | 可以像客戶端輸出內(nèi)容,然而<%= %>更便捷 |
| Object? ?? | page | page = this 指翻譯后當(dāng)前類的對象,很少使用 |
| Throwable? ?? | exception | 錯誤頁面才可使用 |
注:在某個頁面拋出異常時(需頁面定義 errorPage="xxx.jsp"),將轉(zhuǎn)發(fā)至JSP錯誤頁面。提供exception對象是為了在JSP中進行處理,只有在錯誤頁面中才可以使用該對象。所以如果是作為錯誤頁面則必須定義 <%@page isErrorPage="true" %>
那么內(nèi)置對象的使用也很簡單,直接在JSP頁面的<% %>中嵌入即可,如 <%=request.getParameter("title)%>
至于內(nèi)置對象的作用域,從他們的類型來說,已經(jīng)不言而喻了,詳情可以參考Servlet部分的作用域知識點。
轉(zhuǎn)載于:https://www.cnblogs.com/deng-cc/p/8124540.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的[02] JSP内置对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux centos-6.5,Lin
- 下一篇: gradle idea java ssm