Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

XyzWs JSP FAQ:
What is the application implicit object?


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
Advertisement
XyzWs JSP FAQ: What is the application implicit object?

What is the application implicit object?


The application implicit object is an instance of javax.servlet.ServletContext. It is the broadest context state available. It allows the JSP page's servlet and any Web components contained in the same application to share information. A typical use of the application object is to get access to the application wide initialisation parameters. The following two scriptlets are equivalent:

<% String path = application.getRealPath ("/WEB-INF/myfile.txt"); %>

<% String path = getServletContext ().getRealPath ("/WEB-INF/myfile.txt"); %>

According to the javax.servlet.ServletContext APIs document: "Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file."

Do not confuse between the application and config objects. Both of them can be used along with a deployment descriptor (web.xml) that sets up the appropriate initialisation parameters for them, but they have different scope. For example, the following code shows how to use them:

The deployment descriptor file web.xml

<web-app>      
  <context-param>
    <param-name>applparam</param-name>
    <param-value>applvalue</param-value>    
  </context-param> 

  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
    <init-param>
        <param-name>pageparam</param-name>
        <param-value>pagevalue</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/test.jsp</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>TestServlet1</servlet-name>
    <jsp-file>/test1.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestServlet1</servlet-name>
    <url-pattern>/test1.jsp</url-pattern>
  </servlet-mapping>

</web-app>

The file test.jsp

<%
  out.print(application.getInitParameter("applparam"));
  out.print(config.getInitParameter("pageparam"));
%>

The file test1.jsp

<%
  out.print(application.getInitParameter("applparam"));
  out.print(config.getInitParameter("pageparam")); //null
%>

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Support  | Feedback  | Help