
JSP Implicit objects are variables which we can use directly in scriptlets. The web container creates the objects automatically. In the translation phase from JSP to Servlet, the web container creates these objects.
We do not need to explicitly create and we don’t need to declare and initialize because objects are created within service method so that we can access them directly. You can also use these objects inside the JavaBeans and Servlets.
In this article you will learn about JSP implicit object and their types in detail. You can get examples of most of the contained objects from this article.
Table of Contents
Types of JSP Implicit Objects
There are 9 types of implicit objects available.
1) out implicit object
The out implicit object is an instance of a javax.servlet.jsp.JspWriter. We can use out object to write the data to the buffer and send output to the browser.
javax.servlet.jsp.JspWriter has some methods which are as follows:
# | Method | Description |
1 | out.print() | Prints the data type value. |
2 | out.println() | Prints the data type value with new line character. |
3 | out.flush() | Flushes the stream. |
Example of out implicit object
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
int firstValue = 100;
int secondValue = 200;
out.println("firstValue is " + firstValue);
out.println("secondValue is " + secondValue);
%>
</body>
</html>
2) request implicit object
We use this object to get client information. Client information may include form data, cookies, http methods and so on. The request object is an instance of java.servlet.http.HttpServletRequest.
We access the request parameters using the getParameter() method.
Example of request implicit object
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="output.jsp">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
output.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String requestedName = request.getParameter("name");
out.println(requestedName);
%>
</body>
</html>
3) response implicit object
We use this object to set content type, add cookie and redirect to page. This object represents the response that we give to the client.
The response object is an instance of java.servlet.http.HttpServletResponse.
Example of response implicit object
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="output.jsp">
<input type="submit" value="Submit">
</form>
</body>
</html>
output.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
response.sendRedirect("https://geekcer.com");
%>
</body>
</html>
4) config implicit object
You can get configuration information for JSP page, you can get initialization parameter from servlet and you can also get path or file location of JSP. The config object is an instance of javax.servlet.ServletConfig.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>JspTutorials</display-name>
<servlet>
<servlet-name>GeekServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>GeekServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
</web-app>
output.xml
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String servletName = config.getServletName();
out.print("Servlet Name is: " + servletName);
%>
</body>
</html>
5) application implicit object
The Application object is an instance of javax.servlet.ServletContext and when the application is deployed, one instance is created per application.
Example of application implicit object
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Integer counter = (Integer) application.getAttribute("visitorCount");
if (counter == null || counter == 0) {
counter = 1;
} else {
counter = counter + 1;
}
out.println("Visitor: " + counter);
application.setAttribute("visitorCount", counter);
%>
</body>
</html>
6) session implicit object
We can get session information and we can also get, set and remove attributes in the session. We use session object when we need to track client session between client requests then you should go for session object. The session object is an instance of javax.servlet.http.HttpSession.
Example of session implicit object
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
session.setAttribute("name","GeekCer");
%>
<a href="welcome.jsp">Click here to get Name</a>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name = (String)session.getAttribute("name");
out.println("Name is " + name);
%>
</body>
</html>
7) exception implicit object
The exception object is an instance of java.lang.Throwable and we use this object in exception handling to display the error.
Example of exception implicit object
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Exception occured:<%= exception %>
</body>
</html>
8) page implicit object
It is an object that represents the entire JSP page. The page is an instance of the java.lang.object class. The work of page in JSP is similar as this keyword in java. It is an instance of java.lang.Object.
9) pageContext implicit object
It is an instance of javax.servlet.jsp.PageContext. We can use this object to get attribute, get attribute, set attribute and remove attribute at the level of JSP Page, HTTP request, HTTP session and Application level.
In conclusion, in this article you have learned about the jsp Implicit objects in details with their example program. If you have any query regarding this article, you can write to us at geekcer.code@gmail.com. Hope you liked this post about implicit object.
You can freely give your valuable feedback and comments here.