Skip to main content

JSP implicit Object with Example

In JSP implicit objects simplifies the development of web application.
There are 9 JSP implicit objects available in JSP.


1. request
2. response
3. out
4. config
5. Application
6. session
7. page
8. PageContext
9. Exception

let’s take overview of it after that we will understand it with Example



request


This is the HttpServletRequest object associated with the request.

response


This is the HttpServletResponse object associated with the response to the client.

out


This is the PrintWriter object used to write any data to buffer.

session


This is the HttpSession object associated with the request.

application


This is the ServletContext object associated with application context.

config


This is the ServletConfig object associated with the page.

pageContext


This encapsulates use of server-specific features like higher performance JspWriters.

page


This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.

Exception


The Exception object allows the exception data to be accessed by designated JSP.



In case of servlet we have to get this all object using some another object
but in case of JSP this all object are available for each and Every JSP page.

i.e

for Servlet

1
2
 PrintWriter out=resp.getWriter();
 out.println("Hi This is Bhavesh Lakhani");

for JSP

1
2
3
4
5
6
 
<% 
 
out.println("Hi This is Bhavesh Lakhani"); 
 
%>

Available JSP implicit objects and Its Type


implicit-object

Request Implicit object


The request object is type of HttpServletRequest.

which is available in javax.servlet.http.HttpServletRequest package.

For each request container create request object.

The request object provides methods to get form data, cookies, HTTP methods etc.

Methods to get form data.

syntax:

1
2
 
String username = req.getParameter("username");

i.e

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<h1>MyPage</h1>
 
<form action="/requestobject">
 
User name: <input type="text" name="user_name">
<input type="submit" value="Enter">
 
</form>
 
</body>
</html>

RequestObject.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
 
<body>
<%
 
String user=request.getParameter("user_name");
 
%>
 
Hello <%=user %>
 
</body>
</html>

index.html


request

Enter username and Click on enter


2-enter-user-name

output:


3-output

JSP out implicit Object


out is a type of JspWriter.

This is the PrintWriter object used to send output to user.

syntax:

1
2
 
out.println("Hi This is Bhavesh Lakhani");

i.e

outObject.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" %>
 
<html>
<head>
<title>Insert title here</title>
</head>
 
<body>
 
<%
 
out.println("<h1>Hi This is Bhavesh Lakhani</h1>");
 
%>
 
</body>
</html>

Output:

JSP Response implicit Object


The response object is a type of HttpServletResponse.

Which is present in javax.servlet.http.HttpServletResponse

Response object used to Redirect page, Set HTTP status code and add cookies into Response.

container create response object for every Request.

Method of response object.

syntax:

1
2
3
<%
    response.sendRedirect("https://www.google.co.in");
%>

i.e

ResponseObject.JSP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<%
 
   response.sendRedirect("https://www.google.co.in");
 
%>
 
</body>
</html>

Output:

JSP Config implicit Object


  • ServletConfig available in javax.servlet.*; package
  • ServletConfig object is one per servlet class.
  • ServletConfig object is used to pass information to specific servlet or JSP.
  • Object of ServletConfig will be created during initialization process of the servlet.

Let’s Understand it With Example

Project Structure

web.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name>JSPImplicitObject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
	<servlet>
 
	<servlet-name>JSP</servlet-name>
	<jsp-file>/ConfigObject.jsp</jsp-file>
 
	<init-param>
	   <param-name>name</param-name>
	   <param-value>Bhavesh</param-value>
	</init-param>
 
	</servlet>
 
	<servlet-mapping>
	   <servlet-name>JSP</servlet-name>
	   <url-pattern>/JSP</url-pattern>
	</servlet-mapping>
</web-app>

ConfigObject.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<%
 
String name=config.getInitParameter("name");
 
out.println(" Hello "+name);
 
%>
 
</body>
</html>

output:

JSP application implicit Object


  • Application Context is an object of HttpServletContext and It is used pass information to the any Servlet which is present in Application.
  • Application Context object created by container when server started.
  • Application Context object is global to entire web application

Let’s understand it with Example

Project Structure

Application1.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String f_name=application.getInitParameter("f_name");
String l_name=application.getInitParameter("l_name");
 
out.println("<h1>First Name: </h1>"+f_name);
out.println("<h1>Last Name: </h1>"+l_name);
 
%>
</body>
</html>

output:

Application2.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String f_name=application.getInitParameter("f_name");
String l_name=application.getInitParameter("l_name");
 
out.println("<h1>First Name: </h1>"+f_name);
out.println("<h1>Last Name: </h1>"+l_name);
 
%>
 
</body>
</html>

output:

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name>JSPImplicitObject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
 
	<servlet> 
	<servlet-name>JSP</servlet-name>
	<jsp-file>/ConfigObject.jsp</jsp-file>
	</servlet>
 
	<servlet-mapping>
	<servlet-name>JSP</servlet-name>
	<url-pattern>/JSP</url-pattern>
	</servlet-mapping>
 
 
	<context-param>
	<param-name>f_name</param-name>
	<param-value>Bhavesh</param-value>
	</context-param>
 
	<context-param>
	<param-name>l_name</param-name>
	<param-value>Lakhani</param-value>
	</context-param>
 
</web-app>

JSP session implicit object


JSP, session is an implicit object of type HttpSession.

The Developer can use this object to set,get or remove attribute.

Using session object you set and get Information about session.

Let’s understand it with Example.

Project Structure

Createsession.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<h1>Hi This is Bhavesh Lakhani</h1>
<br>
<%
session.setAttribute("username","blakhani2012");
%>
 
<a href="getsession.jsp">Click here to go to Welcome page</a>
 
</body>
</html>

getsession.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String u_name=(String)session.getAttribute("username");
 
out.println("Hello "+u_name);
 
%>
 
</body>
</html>

output:


When you click on hyperlink request is goes to another JSP page where you can get the session which you have created in createsession.jsp

JSP pageContext implicit object


When a JSP page is running you have up to “4 places”
where you can put objects to retrieve them later.

1) Page scope


What you put on your page scope is available only for the JSP page that put it.
Any other page in the same request included via or forwarded will not see object
in the first page scope.

This is the default scope,
so is the same to call

1
2
 
pageContext.setAttribute("a", "b", PageContext.PAGE_SCOPE);

than pageContext.setAttribute(“a”, “b”);

2) Request scope


What you put on your request scope is available across all the request serving this JSP page,
other pages included or forwarded (not HTTP redirect) will share this same context
and see these objects.

Is the same to call

1
2
 
pageContext.setAttribute("a", "b", PageContext.REQUEST_SCOPE);

than request.setAttribute(“a”, “b”);

3) Session scope


What you put on your session scope is available across all requests on the same user session.

Is the same to call

1
pageContext.setAttribute("a", "b", PageContext.SESSION_SCOPE);

than session.setAttribute(“a”, “b”);

4) Application scope


What you put on your application scope is available across all requests on
your application
(i.e. is shared by all users).

Is the same to call

1
2
 
pageContext.setAttribute("a", "b", PageContext.APPLICATION_SCOPE);

than application.setAttribute(“a”, “b”);

Let’s understand it with Example

Project Structure

pageContext.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("user","blakhani2012",pageContext.SESSION_SCOPE); 
 
%>
 
<a href="pageContext_Session.jsp">Click here to go to the welcome page</a>
</body>
</html>

pageContext_Session.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<% 
 
String username=(String)pageContext.getAttribute("user",pageContext.SESSION_SCOPE);  
 
out.println("Hello "+username);
 
%>
</body>
</html>

output:


JSP page implicit object


This is simply a synonym for this, and is used
to call the methods defined by the translated servlet class
.

Page implicit object is a reference to the current Servlet instance
(Converted Servlet, generated during translation phase from a JSP page).
We can simply use this in place of it.

Example:

1
2
 
 this.log("Hi Hello")

JSP Exception Implicit object


  • The exception object represents all errors and exceptions.
  • The exception implicit object is of type java.langThrowable.
  • You can access the exception object on a page thatyou declare to be an error page using the isErrorPage attribute of the page directive.

Note:
The exception object is created only if the JSP uses the page directive to set isErrorPage set to true.
When a JSP generates an error and forwards that error to the error page, the container sets the
JSP exception object of the error page to the generated error

Apart from exception.getMessage(),
you can also invoke printStackTrace() and toString() on exception object.

Let’s try to Understand it with Example

project Structure

ExceptionPage.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<%@ page language="java"%>
<%@ page errorPage="err.jsp" %>
 
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<%
int a=5/0;
 
out.println(a);
 
System.out.print("Exception occur");
 
%>
 
</body>
</html>

err.jsp


1
2
3
4
5
6
7
8
9
10
11
12
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
Sorry something Goes Wrong........
 
</body>
</html>

output:


Download complete project developed using eclipse.



Click here to Download JSPImplicitObject project