Demonstrate JSP implicit object

Q.  Write a JSP application to demonstrate the JSP implicit object.

Answer:

JSP Implicit Object:
JSP implicit objects are created by the Servlet container while translating JSP page to servlet page. There are 9 implicit object present in JSP page. The available implicit objects are out, request, config, session, application etc.

index.jsp

<%@ page language="java"%>
<%@ page import="java.util.Date" %>
<!DOCTYPE html>
    <head>
        <title>Index JSP Page</title>
    </head>
    <body>

        <%-- out object example --%>

        <table border="1"; style="background-color:#ffffcc; width:30%" >
            <caption><h3>JSP Implicit object</h3></caption>
            <tr><td><b>Current Time</b></td>
            <td><% out.print(new Date()); %></td></tr>

            <%-- response object example --%>
            <%response.addCookie(new Cookie("Test","Value")); %>

            <%-- application object example --%>
            <tr><td><b>User context param value</b></td>
            <td><%=application.getInitParameter("User") %></td></tr>

            <%-- session object example --%>
            <tr><td><b>User Session ID</b></td>
            <td><%=session.getId() %></td></tr>

            <%-- pageContext object example --%>
            <% pageContext.setAttribute("Test", "Test Value"); %>
            <tr><td><b>PageContext attribute</b></td>
            <td>{Name="Test",Value="<%=pageContext.getAttribute("Test") %>"}</td></tr>

            <%-- page object example --%>
            <tr><td><b>Generated Servlet Name</b>:</td>
            <td><%=page.getClass().getName() %></td></tr>
    
            <%-- request object example --%>
            <tr><td><b>Request User-Agent</b></td>
            <td> <%=request.getHeader("User-Agent") %></td></tr>
        </table>
    </body>
</html>


web.xml

<web-app>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>  
    <context-param>
        <param-name>User</param-name>
        <param-value>Surendra</param-value>
    </context-param>
    <servlet>
        <servlet-name>xyz</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>xyz</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>


Output:

jsp implicit object