Step 15- JSP IMPLICIT OBJECTS

Learn with our youtube video –

ObjectClassCode Snippet
outRepresents the JSP writer, which can be used to write data to the response.<% out.println(“Hello, world!”); out.print(“Today’s date is: ” + new java.util.Date()); %>
requestRepresents the HTTP request. It can be used to access request parameters and headers.<% String userName = request.getParameter(“username”); out.println(“Hello, ” + userName + “!”); %>
responseRepresents the HTTP response. It can be used to set response headers and cookies.<% response.setHeader(“Content-Type”, “text/html”); response.setIntHeader(“Refresh”, 1); %>
sessionRepresents the HTTP session. It can be used to store data that needs to persist across multiple requests.<% User user = new User(); user.setName(“John Doe”); session.setAttribute(“user”, user); %>
applicationRepresents the ServletContext. It can be used to store data that needs to persist across the entire application.<% List users = new ArrayList(); users.add(new User(“John Doe”)); users.add(new User(“Jane Doe”)); application.setAttribute(“users”, users); %>
exceptionRepresents the exception thrown by the JSP. It can be used to access information about the exception.<%@ page isErrorPage=”true” %> <% out.println(“An error occurred: ” + exception.getMessage()); %>
pageRepresents the current JSP page. It can be used to access page-scoped variables.<% page.setAttribute(“message”, “Hello, world!”); %>
configRepresents the ServletConfig. It can be used to access the initialization parameters of the servlet.<% String dbUrl = config.getInitParameter(“dbUrl”); String dbUsername = config.getInitParameter(“dbUsername”); String dbPassword = config.getInitParameter(“dbPassword”); // use dbUrl, dbUsername, and dbPassword to connect to the database %>
pageContextRepresents the PageContext. It can be used to access various JSP-related objects, such as the ServletContext and the JspWriter.<% JspWriter writer = pageContext.getOut(); writer.println(“Hello, world!”); %>

Code Example –                       

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page language="java" %>
<%@ page import="java.util.*" %>

<html>
<head>
    <title>JSP Implicit Objects Example</title>
</head>
<body>
    <%-- Using out object --%>
    <% out.println("Hello, world!"); %>
    <% out.print("Today's date is: " + new java.util.Date()); %>
    <!-- Equivalent Servlet Object: PrintWriter out = response.getWriter(); -->
    
    <br><br>
    
    <%-- Using request object --%>
    <% String userName = request.getParameter("username"); %>
    <% out.println("Hello, " + userName + "!"); %>
    <!-- Equivalent Servlet Object: HttpServletRequest request -->
    
    <br><br>
    
    <%-- Using response object --%>
    <% response.setHeader("Content-Type", "text/html"); %>
    <% response.setIntHeader("Refresh", 1); %>
    <!-- Equivalent Servlet Object: HttpServletResponse response -->
    
    <br><br>
    
    <%-- Using session object --%>
    <% User user = new User();
    user.setName("John Doe");
    session.setAttribute("user", user); %>
    <!-- Equivalent Servlet Object: HttpSession session -->
    
    <br><br>
    
    <%-- Using application object --%>
    <% List<User> users = new ArrayList<>();
    users.add(new User("John Doe"));
    users.add(new User("Jane Doe"));
    application.setAttribute("users", users); %>
    <!-- Equivalent Servlet Object: ServletContext application -->
    
    <br><br>
    
    <%-- Using exception object --%>
    <%@ page isErrorPage="true" %>
    <% out.println("An error occurred: " + exception.getMessage()); %>
    <!-- Equivalent Servlet Object: Throwable exception -->
    
    <br><br>
    
    <%-- Using page object --%>
    <% page.setAttribute("message", "Hello, world!"); %>
    <!-- Equivalent Servlet Object: None. Page object represents the current JSP page. -->
    
    <br><br>
    
    <%-- Using config object --%>
    <% String dbUrl = config.getInitParameter("dbUrl");
    String dbUsername = config.getInitParameter("dbUsername");
    String dbPassword = config.getInitParameter("dbPassword");
    // Use dbUrl, dbUsername, and dbPassword to connect to the database %>
    <!-- Equivalent Servlet Object: ServletConfig config -->
    
    <br><br>
    
    <%-- Using pageContext object --%>
    <% JspWriter writer = pageContext.getOut();
    writer.println("Hello, world!"); %>
    <!-- Equivalent Servlet Object: PageContext pageContext -->
    
</body>
</html>