It's not a secret anymore!
The moose likes JSP and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
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: 1580
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"
jQuery in Action, 3rd edition