JSP Impilicit Objects - application


JSP application -

application is an object of javax.servlet.ServletContext. In servlet we can get it by using getServletContext() method, whereas in JSP it is automatically available.

Example -

This basic example will show you how to use application object in JSP file to get context parameters defined in web.xml.

1- index.html or index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>JSP application Demo</title>
    </head>
    <body>
        <h1>ServletContext Demo</h1>
        <a href="First.jsp">Click here to get ServletCotext info </a>
    </body>
</html>        

2- web.xml - In web.xml file add the following context attributes -
<?xml version="1.0" encoding="UTF-8"?>
<web-app>

<context-param>
        <param-name>Name</param-name>
        <param-value>Tom</param-value>
    </context-param>
    <context-param>
        <param-name>Email</param-name>
        <param-value>Tom@gettechnotes.com</param-value>
    </context-param>
</web-app>        

3- Create a JSP file named First, where we will use application implicit object to get context parameters details defined in web.xml.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Context Parameter Details - </h1>
        <%
         String nm = application.getInitParameter("Name"); 
         String email = application.getInitParameter("Email");
         out.println("Name is - "+ nm + "<br>");
         out.println("Email is - "+ email + "<br>");
         
         out.println("Parameters names are - ");
         java.util.Enumeration<String> enu = application.getInitParameterNames();
         while(enu.hasMoreElements())
         {
             out.println(enu.nextElement() + "<br>");
         }
        %>
    </body>
</html>        

3- Now run the project and it will show you the default page as follows -



Click on the link and it will show you context parameter details as follows -



Download this example (NetBeans implementation) - Click Here