JSP Implicit Object

What is implicit Object in JSP ?

JSP Implicit Objects are the Java objects which is created by JSP container.Implicit objects need not to be declared, defined or instantiated like other JAVA Objects.Developer can use these objects by directly calling it by name.

JSP supports nine Implicit Objects which are listed below:

  1. request
  2. response
  3. out
  4. session
  5. application
  6. config
  7. pageContext
  8. page
  9. exception

What is implicit Object in JSP ?

JSP Implicit Objects are the Java objects which is created by JSP container.Implicit objects need not to be declared, defined or instantiated like other JAVA Objects.Developer can use these objects by directly calling it by name.

JSP supports nine Implicit Objects which are listed below:

  1. request
  2. response
  3. out
  4. session
  5. application
  6. config
  7. pageContext
  8. page
  9. exception



1 : request Object :

The request object is an instance of javax.servlet.http.HttpServletRequest object. Each time a client requests a page the JSP engine creates a new object to represent that request.This object has a scope for that specific request only.

Example :

1) 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>
<title>JSP Request Object Example</title>
</head>
<body>
	<a href="info.jsp?id=5">JSP Request Object Example</a>
</body>
</html>

2) info.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>
<title>JSP Request Object Example</title>
</head>
<body>
	Id is:	<%=request.getParameter("id")%>
</body>
</html>

Output in Browser:

ID is:5

2 : response Object :

This object implements javax.servlet.ServletResponse interface. It is useful object in setting response to serve the request. This object has a scope for that specific request.

Example :

1) index.jsp

<html>
<head>
<title>JSP Response Object Example</title>
</head>
<body>
<%  
	PrintWriter pw =  response.getWriter();
 	pw.print("Hello This is an example of JSP 'Response' object");
%>
</body>
</html>

Output in Browser :

Hello This is an example of JSP 'Response' object

3 : out Object :

The out implicit object is an instance of javax.servlet.jsp.JspWriter object and is used to send content in a response.

Example :

1) index.jsp

<html>
<head>
<title>JSP out Object Example</title>
</head>
<body>
<%  
	out.println("Hello Boss");
%>
</body>
</html>

Output in Browser :

Hello Boss

4 : session Object :

Session object is useful in maintaining objects and accessing it values in different JSPs. Session object has a scope of session.

Example :

1) firstpage.jsp

<html>
<head>
<title>JSP Session Object Example</title>
</head>
<body>
	<% 
		session.setAttribute("item", "Book");
	%>
	<a href="secondpage.jsp">JSP Session Example</a>
</body>
</html>

2) secondpage.jsp

<html>
<head>
<title>JSP Session Object Example</title>
</head>
<body>
	Session attribute value: <%=session.getAttribute("item")%>
</body>
</html>

Output in Browser :

Session attribute value: Book

In the above code, firstpage.jsp has a link that points to another JSP secondpage.jsp . In first.jsp , we set the session attribute ' item ' with value as ' Book'. You can save any object in session using setAttribute method of session object. In next JSP, we can read the variable set during current session using getAttrubute method of session object.

5 : application Object :

application object is useful in maintaining objects and accessing it values in different JSPs. application object has a scope of entire application.

Example :

1) firstpage.jsp

<html>
<head>
<title>JSP Application Object Example</title>
</head>
<body>
<% 
 application.setAttribute("name", "Mukesh");
%>
<a href="test.jsp">JSP Application Example</a>
</body>
</html>

2) secondpage.jsp

<html>
<head>
<title>JSP Application Object Example</title>
</head>
<body>
	Application attribute value:
	<%=application.getAttribute("name")%>
</body>
</html>

Output in Browser :

Application attribute value: Mukesh

As you can see above, firstpage.JSP has a link that points to another JSP secondpage.jsp . On that JSP, we set the application attribute ' name ' with value as 'Mukesh '. You can save any object in application using setAttribute method of application object. On next JSP, we can read the variable set during current application using getAttrubute method of application object.

6 : config Object :

The config object is an instantiation of javax.servlet.ServletConfig.Config object is useful in getting application configuration values to JSP from web.xml file. Config object has a scope of application.

Example :

1) web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>JSP</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>ConfigObjectTest</servlet-name>
		<jsp-file>/demo.jsp</jsp-file>
		<init-param>
			<param-name>name</param-name>
			<param-value>Mukesh</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>ConfigObjectTest</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

2) info.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>
<title>JSP Application Object Example</title>
</head>
<body>
	Config Value for Parameter 'name' is: 
	<%= config.getInitParameter("name")%>
</body>
</html>

Output in Browser :

Config Value for Parameter 'name' is: Mukesh

pageContext Object :

The pageContext object is an instance of a javax.servlet.jsp.PageContext object. This object is used to represent the entire JSP page.

page Object :

This object is an actual reference to the instance of the page.The page object is like this object.

exception Object :

This object is wrapper containing the exception thrown from the previous page. It is typically used to generate an appropriate response to the error condition.





comments powered by Disqus