The Application Object in JSP

By: Sathya Narayana Viewed: 172 times  Printer Friendly Format    


The application object is an instance of a class that implements the javax.servlet.ServletContext interface, and it allows the page to obtain and to set data information about the Web application in which it's running. It makes available the following methods.

getAttribute() returns the value of the specified attribute name. The return value is an Object or subclass if the attribute is available to the invoking ServletContext object or null if the attribute isn't available:

public Object getAttribute(String name)

getAttributeNames() returns an Enumeration containing the attribute names available to the invoking ServletContext object:

public java.util.Enumeration getAttributeNames()

getContext() returns the ServletContext object for the resource at the specified path on the server. The path argument is an absolute URL beginning with /:

public ServletContext getContext(String uripath)

getInitParameter() returns a String object containing the value of the specified initialization parameter or null if the parameter doesn't exist:

public String getInitParameter(String name)

getInitParameterNames() returns a Enumeration containing the initialization parameters associated with the invoking ServletContext object:

public java.util.Enumeration getInitParameterNames()

getMajorVersion() returns the major version of the Java servlet API that the server supports. For servers supporting version 2.3 of the servlet specification, this method will return 2:

public int getMajorVersion()

getMimeType() returns the MIME type of the specified file or null if the MIME type can't be ascertained. Typical return values will be "text/plain", "text/html", and "image/jpg":

public String getMimeType(String file)


getMinorVersion() returns the minor version of the Java servlet API that the server supports. For servers supporting version 2.3 of the servlet specification, this method will return 3:

public int getMinorVersion()

getNamedDispatcher() returns a RequestDispatcher object that will be wrapped around the named servlet:

public RequestDispatcher getNamedDispatcher(String name)

getRealPath() returns a String object containing the real path, in a form appropriate to the platform on which the servlet is running, corresponding to the given virtual path. An example of a virtual path might be "/blah.html":

public String getRealPath(String path)

getRequestDispatcher() returns a RequestDispatcher object that acts as a wrapper around the resource located at the specified path. The path must begin with / and is interpreted relative to the current context root:

public RequestDispatcher getRequestDispatcher(String path)

getResource() returns a URL object that's mapped to the specified path or null if there's no resource mapped to the path. The path must begin with / and is interpreted relative to the current context root:

public java.net.URL getResource(String path)
throws java.net.MalformedURLException

getResourceAsStream() returns the resource at the specified path as an InputStream object:

public java.io.InputStream getResourceAsStream(String path)

getResourcePaths() returns all the paths to resources held in the Web application as String objects beginning with a /:

public java.util.Set getResourcePaths()

getServerInfo() returns a String object containing information about the server on which the servlet is running. At a minimum, the String will contain the servlet container name and version number:

public String getServerInfo()

The following will display Apache Tomcat/5.0.16 for Tomcat 5.0.16:

<% out.print(application.getServerInfo()); %>

getServletContextName() returns the name of the Web application, as specified in the <display-name> element in web.xml:

public String getServletContextName()

log() is used to write a message to the servlet engine's log file. The second version writes both an explanatory message and a stack trace for the specified Throwable exception to the log file:

public void log(String msg)
public void log(String message,
Throwable throwable)

removeAttribute() makes the specified attribute unavailable to the invoking ServletContext object. Subsequent calls to the getAttribute() method for this attribute will return null:

public void removeAttribute(String name)

setAttribute() binds a value to a specified attribute name:

public void setAttribute(String name,
Object object)

These methods are deprecated and shouldn't be used in new code—they exist for compatibility with existing code:

public Servlet getServlet(String name)
throws ServletException
public java.util.Enumeration getServletNames()
public java.util.Enumeration getServlets()

public void log(Exception exception,
String msg)




Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial