JSP Implicit Objects: Complete Tutorial

What is JSP Implicit object?

How many Implicit Objects are available in JSP?

There are 9 types of implicit objects available in the container:

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

Lets study One By One

out

Example:

<%@ 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>Implicit Guru JSP1</title>
</head>
<body>
<% int num1=10;int num2=20;
out.println("num1 is " +num1);
out.println("num2 is "+num2);
%>
</body>
</html>

Explanation of the code:

Code Line 11-12– out is used to print into output stream

When we execute the above code, we get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

Request

Example:

Implicit_jsp2.jsp(form from which request is sent to guru.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>Implicit Guru form JSP2</title>
</head>
<body>
<form action="guru.jsp">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>

Guru.jsp (where the action is taken)

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Explanation of code:

Code Line 10-13 : In implicit_jsp2.jsp(form) request is sent, hence the variable username is processed and sent to guru.jsp which is action of JSP.

Guru.jsp

Code Line10-11: It is action jsp where the request is processed, and username is taken from form jsp.

When you execute the above code, you get the following output

Output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you write test and click on the submit button, then you get the following output "Welcome Test."

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Response

Example:

<%@ 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>Implicit Guru JSP4</title>
</head>
<body>
<%response.setContentType("text/html"); %>
</body>
</html>

Explanation of the code:

Code Line 11: In the response object we can set the content type

Here we are setting only the content type in the response object. Hence, there is no output for this.

Config

Example:

Web.xml (specifies the name and mapping of the servlet)

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Implicit_jsp5.jsp (getting the value of servlet name)

<%@ 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>Implicit Guru JSP5</title>
</head>
<body>
<% String servletName = config.getServletName();
out.println("Servlet Name is " +servletName);%>
</body>
</html>

Explanation of the code:

In web.xml

Code Line 14-17: In web.xml we have mapping of servlets to the classes.

Implicit_jsp5.jsp

Code Line 10-11: To get the name of the servlet in JSP, we can use config.getServletName, which will help us to get the name of the servlet.

When you execute the above code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

Application

Example:

<%@ 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>Guru Implicit JSP6</title>
</head>
<body>
<% application.getContextPath(); %>
</body>
</html>

Explanation of the code:

Session

Example:

Implicit_jsp7(attribute is set)

<%@ 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>Implicit JSP</title>
</head>
<body>
<% session.setAttribute("user","GuruJSP"); %>
<a href="implicit_jsp8.jsp">Click here to get user name</a>
</body>
</html>

Implicit_jsp8.jsp (getAttribute)

<%@ 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>implicit Guru JSP8</title>
</head>
<body>
<% String name = (String)session.getAttribute("user");
out.println("User Name is " +name);
%>
</body>
</html>

Explanation of the code:

Implicit_jsp7.jsp

Code Line 11: we are setting the attribute user in the session variable, and that value can be fetched from the session in whichever jsp is called from that (_jsp8.jsp).

Code Line 12: We are calling another jsp on href in which we will get the value for attribute user which is set.

Implicit_jsp8.jsp

Code Line 11: We are getting the value of user attribute from session object and displaying that value

When you execute the above code, you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you click on the link for the username. You will get the following output.

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

pageContext:

Scopes are of 4 types:

Example:

<%@ 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>Implicit Guru JSP9</title>
</head>
<body>
<% pageContext.setAttribute("student","gurustudent",pageContext.PAGE_SCOPE);
String name = (String)pageContext.getAttribute("student");
out.println("student name is " +name);
%>
</body>
</html>

Explanation of the code:

Code Line 11: we are setting the attribute using pageContext object, and it has three parameters:

In the above code, the key is student and value is "gurustudent" while the scope is the page scope. Here the scope is "page" and it can get using page scope only.

Code Line 12: We are getting the value of the attribute using pageContext

When you execute the above code, you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

Page

Example:

In this example, we are using page object to get the page name using toString method

<%@ 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>Implicit Guru JSP10</title>
</head>
<body>
<% String pageName = page.toString();
out.println("Page Name is " +pageName);%>
</body>
</html>

Explanation of the code:

Code Line 10-11: In this example, we are trying to use the method toString() of the page object and trying to get the string name of theJSP Page.

When you execute the code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

Exception

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true"%>
<!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>Implicit Guru JSP 11</title>
</head>
<body>
<%int[] num1={1,2,3,4};
out.println(num1[5]);%>
<%= exception %>
</body>
</html>

Explanation of the code:

Code Line 10-12 - It has an array of numbers, i.e., num1 with four elements. In the output, we are trying to print the fifth element of the array from num1, which is not declared in the array list. So it is used to get exception object of the jsp.

Output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

We are getting ArrayIndexOfBoundsException in the array where we are getting a num1 array of the fifth element.

 

YOU MIGHT LIKE: