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:
- out object
- request object
- response object
- pageContext object
- page object
- exception object
- config object
- application object
- 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
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
After providing a name (for example Sandeep) a new welcome message is generated as in the following:
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
After pressing the MyhomePage button we see the following: