This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark ""application" and "getServeltContext"" Watch ""application" and "getServeltContext"" New topic

"application" and "getServeltContext"

Lei Xiaowen

Joined: Jul 11, 2005
Posts: 15
Can you tell me the difference between "getServletContext()" and "application"?

The code is as follows:
public void jspInit()
getServletContext().setAttribute("cats",EShop.getCats())//why getServletContext() use here, and can we raplace it with application ?
public void jspDestroy(){........}

Best wishes,
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There is no difference.

The implicit variable "application" is there for convenience so you don't need to call getServletContext().

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Stefan Evans

Joined: Jul 06, 2005
Posts: 1016
Correct me if I'm wrong, but aren't the implicit variables only declared for the _jspService() method? ie usable only in <% jsp code %> and <%= jspExpression %>
This is code written in a <%! jsp declaration %> and so doesn't have access to the implicit variables.
That would explain the call to getServletContext().

In fact to be completely compliant it should probably be getServletConfig().getServletContext(). The JSP interface doesn't mandate the implementation of the getServletContext() method.
[ September 17, 2006: Message edited by: Stefan Evans ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Good point.
The implicit variables are not available in outside of the service method.
I agree. Here's the link:
subject: "application" and "getServeltContext"
Similar Threads
application scope in servlet
Servlets and ServletContext
j2eecertificate mock exam question
Mock question