What is Implicit Object in JSP? In this article, we will look at the request, response, session, config, application, pageContext, page, Exception, and out objects in JSP with examples.
Implicit Objects In JSP
Implicit objects in JSP are created by the web container. There are 9 implicit objects in JSP.
- request
- response
- session
- config
- application
- pageContext
- page
- exception
- out
Request Objects in JSP
- The request object is an instance of the class which is implements java .servlet.http.HttpServletRequest interface.
- Created by the container for every request.
- request Object is used to request information like header information, parameter, server name.
- getParameter() method is used to access the request from the request object.
<% String name=request.getParameter("name"); out.print("welcome "+name); %>
Response Objects in JSP
- The response is an instance of the class which implements the HttpServletResponse interface.
- It will be created by the container for each request.
- It will help to send a response to the client.
- Mostly it is used to redirect to the response page and add the cookie.
<% response.sendRedirect("https://www.codebun.com"); %>
Session object in JSP
- The session is holding “httpsession” object.
- A session object is used to get, set, and remove attributes to the session scope and also used to get session information.
For example, in A real-time example when a user login to a website a session object is created for that user, and the complete profile of that user will be managed by the session object.
How to Set a session in JSP?
<% String name=request.getParameter("uname"); session.setAttribute("session_name",name); %>
How to get a session at any page in the application in JSP?
<% String name=(String)session.getAttribute("session_name"); out.print("Hello "+name); %>
Config object in JSP
- “config” is of the type java.servlet.servletConfig.
- A ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.
- This object can be used to get configuration information from a web.xml file.
Example of config object in JSP (web.xml)
<init-param> <param-name>var_name</param-name> <param-value>Welcome to codebun.com</param-value> </init-param>
index.jsp
<% String message=config.getInitParameter("var_name"); out.print("driver name is="+message); %>
Application object in JSP.
- The application object is an instance of javax.servlet.ServletContext.
- It is used to get the context information and attributes in JSP.
- Application object is created by the container.
- Servletcontext object contains a set of methods that are used to interact with the servlet container.
- We can find information about the servlet container.
web.xml
<context-param> <param-name>var_name</param-name> <param-value>Welcome to codebun.com</param-value> </context-param>
index.jsp
<% String message=application.getInitParameter("var_name"); out.print("driver name is="+message); %>
pageContext object in JSP.
- This object is of the type of pagecontext.
- It is used to get, set, and remove the attributes from a particular scope.
There are four types of objects.
- Page
- Request
- Session
- Application
<% pageContext.setAttribute("key_var","Value : hello JSP Welcome to codebun",pageContext.PAGE_SCOPE); String msg= (String)pageContext.getAttribute("key_var"); out.println("Message : " +msg); %>
Page object in JSP.
- Page implicit variable holds the currently executed servlet object for the corresponding JSP.
- Type of page is Object.
- Acts as this object for the current JSP page.
<% String page_name = page.toString(); out.println("Page Name is " +page_name);%>
Exception object in JSP.
- An exception is the implicit object of the Throwable class.
- It is used for exception handling in JSP.
<%@ page isErrorPage="true" %> <html> <body> Exception occured:<%= exception %> </body> </html>
out Object in JSP.
Out is object of javax.servlet.jsp.jspWriter class.
- Out is one of the implicit objects to send output to the client as a response.
- Out object allows us to access the servlet’s output stream.
- In Jsp web don’t need to create an object of PrintWriter but in Servlet we need to define the object of printWriter like“PrintWriter out=response.getWriter(); “
<html> <body> <% out.print("Welcome to codebun.com! Enjoy learning"); %> </body> </html>