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
- request : Represents the HttpServletRequest object.
- response : Represents the HttpServletResponse object.
- out : Represents the JspWriter object used to send content to the client.
- session : Represents the HttpSession object.
- application : Represents the ServletContext object.
- config : Represents the ServletConfig object.
- pageContext : Provides access to all the namespaces associated with a JSP page.
- page : Refers to the current JSP page as a Servlet instance.
- exception : Represents the Throwable object containing the exception, available in error pages.
Table of Contents
1. request
The request object is used to get information about the request made by the client.
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.
jsp
<%
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
%>
3. out
The out object is used to send content to the client.
jsp
<%
out.println("Welcome to JSP!");
%>
4. session
The session object is used to store and retrieve information about the user’s session.
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.
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.
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.
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.
jsp
<%
out.println("Current Page: " + page.getClass().getName());
%>
9. exception
The exception object is used in error pages to get information about the exception.
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.