ServletContext理解與application異同.
而且application的作用域是整個(gè)Tomcat啟動(dòng)的過(guò)程。
例如: ServletContext.setAttribute("username",username);
則在JSP網(wǎng)頁(yè)中可以使用
application.getAttribute("username");
來(lái)得到這個(gè)用戶名。相同:其實(shí)servletContext和application是一樣的,就相當(dāng)于一個(gè)類創(chuàng)建了兩個(gè)不同名稱的變量。在
servlet中ServletContext就是application對(duì)象。大家只要打開jsp編譯過(guò)后生成的Servlet中的
_jspService()方法就可以看到如下的聲明:
ServletContextapplication = null;
application= pageContext.getServletContext();
不同:兩者的區(qū)別就是application用在jsp中,servletContext用在servlet中。application和page
requestsession都是JSP中的內(nèi)置對(duì)象,在后臺(tái)用ServletContext存儲(chǔ)的屬性數(shù)據(jù)可以用
application對(duì)象獲得。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。