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
Author

"application" and "getServeltContext"

Lei Xiaowen
Greenhorn

Joined: Jul 11, 2005
Posts: 15
Hi,All
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,
LeiXiaowen
Ben Souther
Sheriff

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
Bartender

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
Sheriff

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: http://aspose.com/file-tools
 
subject: "application" and "getServeltContext"
 
Similar Threads
Context
application scope in servlet
Servlets and ServletContext
j2eecertificate mock exam question
Mock question