JSP Implicit Objects

Implicit objects are java objects that are created by container when JSP page is being translated to servlet and are accessible to Java scriptlets or expressions in JSP pages based on scope of particular object type.

There are 9 implicit objects which are listed below:

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

out

It is used to send content or output to the client.It is instance of javax.servlet.jsp.JspWriter object.

Following example demonstrates use of this attribute:
Listing 1: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing out Implicit object</h2>
<% out.println("today's date is:"+new java.util.Date());
%>
</body>
</html>

Output
Execute the above example.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_outobject_demo

request

It is an instance of javax.servlet.http.HttpServletRequest object. Whenever a client requests a page, JSP engine creates new object to that request.

Following example demonstrates use of this attribute:
Listing 2: example.html

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hello.jsp">
<input type="text" name="username"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Now create jsp file as hello.jsp. This is file which is defined in the action attribute in the form element of above html file.

Listing 3: hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing request Implicit object</h2>
<%
String name=request.getParameter("username");
out.println("Welcome "+ name);
%>
</body>
</html>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_requestobject1_demo

Now enter some text. I entered “Javabeat” and clicked on submit button, the output is:
jsp_requestobject2_demo

response

It is an instance of javax.servlet.http.HttpServletResponse object. Server creates an object to response as it creates a request object. Through this object user can add new cookies or date stamps, HTTP status codes etc to your JSP programme.
Following example demonstrates use of this attribute:
Listing 4: example.html

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="response.jsp">
<input type="text" name="username"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Create jsp file as response.jsp.
Listing 5: response.jsp

<html>
<body>
<%
response.sendRedirect("http://www.yahoo.com");
%>
</body>
</html>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_requestobject1_demo

Now enter some text. I entered “Javabeat” and clicked on submit button, the browser is redirected to yahoo.com page.

session

It is an instance of javax.servlet.http.HttpSession. It is used to set or get session information. Whenever we request for jsp page, container will create session for that jsp.

Following example demonstrates use of this attribute:
Listing 6: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing session Implicit object</h2>
<%=session.getId()%>
</body>
</html>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_session_demo

application

The application object is direct wrapper around the ServletContext object. It is used to get information and attributes in JSP. It is also used to forward the request to another resource or to include the response from another resource.
Following example demonstrates use of this attribute:
Listing 7: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing application Implicit object</h2>
<%
String driver=application.getInitParameter("dbname");
out.print("driver name is="+driver);

%>
</body>
</html>

Define this parameter in web.xml as below:
Listing 8: 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>HelloWorldJSP</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>

  </welcome-file-list>
  <context-param>
	<param-name>dbname</param-name>
	<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_application_demo

config

This object is an instance of javax.servlet.ServletConfig and is a direct wrapper around the ServletConfig object for the generated servlet. It is used to get configuaration message for jsp page. Also it is used to get the init parameter present in deployment descriptor.

pagecontext

It is an instance of a javax.servlet.jsp.PageContext object. It represents entire JSP page. It store references to reuqests and repsonses. You can derive application, config, session, and out objects from pagecontext object. pagecontext object consist of four scope fields amongst other fields:

  • PAGE_SCOPE
  • REQUEST_SCOPE
  • SESSION_SCOPE
  • APPLICATION_SCOPE

Following example demonstrates use of this attribute:
Listing 10: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing pagecontext Implicit object</h2>
<% pageContext.setAttribute("param1", "Javabeat"); %>
PageContext attribute:{Name="someName",Value="<%=pageContext.getAttribute("param1") %>"}

</body>
</html>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:

jsp_pagecontext_demo

page

This object can be thoguht of repreenting an etire JSP page. It is rarely used.
Following example demonstrates use of this attribute:
Listing 11: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body><h2>Testing page Implicit object</h2>
<%=page.getClass().getName() %>

</body>
</html>

Output
Execute the above example.html in Eclipse by selecting Run As > Run On Server, output as below would be seen:
jsp_page_demo

exception

It is used to display exception in jsp error pages. This object is a wrapper containing the exception thrown from the previous page. Youc an an example for this in the Exception Handling in JSP chapter.

Previous Tutorial :  JSP Directives   ||  Next Tutorial :  JSP Redirect

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better