淺談jsp EL表達式取值過程、page和pagecontext的區(qū)別
1、EL表達式解析過程
JSP中,我們經(jīng)常會寫為${obj.name}字樣,但你有沒有想過,它的取值過程是什么,屬性值從哪取得?
${obj}相當于 request.getAttribute("obj"),這句話嚴格來說不嚴謹,依次的請求范圍是page、request、session、application
也就是說,如果在page.getAttribute()找不到,再去request.getAttribute,如果request找不到,再去session里找,session里找不到,再去application里
page.getAttribute-------->request.getAttribute------------>session.getAttribute----------->applicaton.getAttribute
2、page和pagecontext的區(qū)別
page就是當前jsp頁面,也等同于jsp編譯后的servlet,查看java代碼可以得知,page是java.lang.Object類型
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
<span style="white-space:pre"> </span>null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
jspx_out = out;<span style="font-family: Arial, Helvetica, sans-serif;"> }</span>
來個具體的例子吧。
page1.jsp,設(shè)置鍵值對
<%
page.setAttribute("name","obma")
%>
在page1.jsp,可以取出上邊設(shè)置的name值,但是在其它頁面(page2,page3中....)獲取到的都是null
<%
String value = (String)page.getAttribute("name");
%>
pagecontext,是page的上下文,是javax.servlet.jsp.PageContext類型,它持有request,response,也持有page,通過pagecontext可以獲取servletcontext、servletconfig等,可以看出他是一個橋梁可以獲取上下文變量
以上這篇(標題)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JSP FusionCharts Free顯示圖表 具體實現(xiàn)
這篇文章介紹了JSP FusionCharts Free顯示圖表的具體實現(xiàn),有需要的朋友可以參考一下2013-07-07
SSM框架JSP使用Layui實現(xiàn)layer彈出層效果
這篇文章主要介紹了SSM框架JSP使用Layui實現(xiàn)layer彈出層效果,文章通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
FCKeditor使用方法(FCKeditor_2.6.3)詳細使用說明
要用到文本編輯器,選擇了FCKeditor,下面就配置作一下說明: 環(huán)境:windowsXP myeclipse6.0GA fckeditor2.6.3 fckeditor2.32008-09-09

