JSP Implicit Object Part-1

Introduction

This article describes implicit objects of JSP as Out, request and response objects. We define the rest of the objects in the next article.

JSP Implicit objects

There are 9 types of JSP implicit objects. The auto generated servlet contains many objects like out, request, config, session, application etcetera. The 9 implicit objects are as follows:

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

1. out object

JSP provides an implicit object called out, for writing any data to the buffer.

Example

In this example, we simply display a message ("Welcome To JSP World").

simple.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>Out implicit ex</title>

</head>

<body>

<% out.print("Welcome To JSP World"); %> 

</body>

</html>

Output

fig-1.jpg

2. Request implicit object

HttpServletRequest provides a request implicit object. 

Example

In this example, we are displaying the name of the user with a welcome message.

request.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=ISO-8859-1">

<title>Request implicit Example</title>

</head>

<body>

<form action="display.jsp">

<input type="text" name="uname">

<input type="submit"  value="go"><br/>

</form>

</body>

</html>

display.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>Request Processing</title>

</head>

<body>

<%

String name=request.getParameter("uname");

out.println("Welcome "+name);

%>

</body>

</html>

Output

Fig-2.jpg

After providing a name (for example Sandeep) a new welcome message is generated as in the following:

Fig-3.jpg

3. response implicit object

In JSP, response is an implicit object of type HttpServletResponse. Let's see an example of a response implicit object where we are redirecting the request to the http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx

Example

In this example, when we click on the MyhomePage button a new window is generated that shows my home page on c-sharpcorner.com.

response.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=ISO-8859-1">

<title>Response implicit ex</title>

</head>

<body>

<form action="response.jsp">

<input type="submit" value="MyhomePage"><br/>

</form>

</body>

</html>

response.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>Response shows here</title>

</head>

<body>

<% response.sendRedirect("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx"); %>

</body>

</html>

Output

Fig-4.jpg


After pressing the MyhomePage button we see the following:

Fig-5.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all