Implicit Objects used with basic example

Implicit Objects used with basic example

In JSP, implicit objects are pre-defined variables that provide access to various objects related to the request, response, session, and application. These objects are automatically available in JSP pages without the need for explicit declaration.

List of Implicit Objects

  1.  request : Represents the HttpServletRequest object.
  2.  response : Represents the HttpServletResponse object.
  3.  out : Represents the JspWriter object used to send content to the client.
  4.  session : Represents the HttpSession object.
  5.  application : Represents the ServletContext object.
  6.  config : Represents the ServletConfig object.
  7.  pageContext : Provides access to all the namespaces associated with a JSP page.
  8.  page : Refers to the current JSP page as a Servlet instance.
  9.  exception : Represents the Throwable object containing the exception, available in error pages.

Implicit Objects

1. request

The request object is used to get information about the request made by the client.

Example
jsp
<% 
    String user = request.getParameter("username");
    out.println("Hello, " + user);
%>

2. response

The response object is used to manipulate the response sent to the client.

Example
jsp
<%
    response.setContentType("text/html");
    response.setHeader("Cache-Control", "no-cache");
%>


3. out

The out object is used to send content to the client.

Example
jsp
<%
    out.println("Welcome to JSP!");
%>

4. session

The session object is used to store and retrieve information about the user’s session.

Example
jsp
<%
    session.setAttribute("user", "John Doe");
    String user = (String) session.getAttribute("user");
    out.println("User: " + user);
%>


5. application

The application object is used to share information among all servlets and JSP pages in the application.

Example
jsp
<%
    application.setAttribute("appName", "My Web Application");
    String appName = (String) application.getAttribute("appName");
    out.println("Application Name: " + appName);
%>

6. config

The config object is used to get the servlet configuration information.

Example
jsp
<%
    String servletName = config.getServletName();
    out.println("Servlet Name: " + servletName);
%>

7. pageContext

The pageContext object is used to get information about the page and manage various scopes.

Example
jsp
<%
    pageContext.setAttribute("pageAttr", "This is a page attribute");
    String pageAttr = (String) pageContext.getAttribute("pageAttr");
    out.println("Page Attribute: " + pageAttr);
%>

8. page

The page object is a reference to the current servlet instance.

Example
jsp
<%
    out.println("Current Page: " + page.getClass().getName());
%>

9. exception

The exception object is used in error pages to get information about the exception.

Example
jsp
<%@ page isErrorPage="true" %>
<%
    out.println("Error: " + exception.getMessage());
%>

Summary

Implicit objects in JSP provide a convenient way to access various aspects of the request, response, session, application, and page without the need for explicit declarations. Each object serves a specific purpose, making it easier to handle different parts of the web application’s lifecycle and user interactions.

Homepage

Readmore