wood burning stoves 2.0*
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 Java 8 in Action this week in the Java 8 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
Mock question
j2eecertificate mock exam question
Context
Servlets and ServletContext
application scope in servlet