JSP Implicit object with Examples

In this article, we will see JSP Implicit object with Examples,

There are total 9 Implicit Objects in JSP, they are below as follows,

1. Out Object

2. request Object

3. response Object

4. config Object

5. application Object

6. session Object

7. pageContext Object

8. page Object

9. exception Object

Let's see one by one implicit Object in detail with Examples ...

1. out Object:

JSP out implicit object is instance of javax.servlet.jsp.JspWriter implementation and it’s used to output content to be sent in client response. This is one of the most used JSP implicit objects and that's why we have JSP Expression to easily invoke out.print() method. In the case of Servlet we need to write:

Syntax:

PrintWriter out=response.getWriter();


But in JSP, you do not need to write this code. Because in JSP for print any result on the browser we use expression tag and this tag is auto-converted to out.print() statement and insert the out.print() into _jspService() of the servlet class by the container.

<%@ 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>Insert title here</title>

</head>

<body>

       <%

             out.print("Welcome");

       %>

 

</body>

</html>



Eclipse Code:





2. request Object

JSP request implicit object is instance of javax.servlet.http.HttpServletRequest implementation and it’s one of the arguments of the JSP service method. We can use request object to get the request parameters, cookies, request attributes, session, header information, and other details about the client request. 

Example of JSP request implicit Object

JSP File 1. Input.jsp
Here we will create Output.jsp to request other JSP file to print the action form.

<%@ 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>Insert title here</title>

</head>

<body>

<form action="Output.jsp">

       <input type="text" name="uname"> <input type="submit" value="go"> <br />

</form>

</body>

</html>


Eclipse Code:

Output:
This is Input.jsp file output which will ask to enter input then after clicking "go" button, it will request Output.jsp file to get the output.


JSP File 2: Output.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>Insert title here</title>

</head>

<body>

<%

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

out.print("welcome "+name);

%>

</body>

</html>


Eclipse Code: 
Output.jsp

Output:

3. response Object

JSP response implicit object is instance of javax.servlet.http.HttpServletResponse implementation and comes as an argument of the service method. We can response object to set content type, character encoding, header information in response, adding cookies to response and redirecting the request to other resource.

Example of response implicit object: 

<%@ 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>Insert title here</title>

</head>

<body>

<%

response.sendRedirect("https://javatechseries.blogspot.com");

%>

</body>

</html>


Eclipse Code:


Output:
4. config Object

JSP config implicit object is instance of javax.servlet.ServletConfig implementation and used to get the JSP init params configured in deployment descriptor. config object is an implicit object of type ServletConfig and it is created by the container, whenever servlet object is created. This object can be used to get initialization parameter for a particular JSP page. config object is created by the web container for every jsp page. It means if a web application has three jsp pages then three config object are created. In jsp, it is not mandatory to configure in web.xml. If we configure a jsp in web.xml then the logical name and init parameter given in web.xml file are stored into config object by the container. config object is accessible, only if the request is given to the jsp by using its url pattern, but not with name of the jsp. config object is accessible, only if the request is given to the jsp by using its url pattern, but not with name of the jsp

Example of config implicit object: 

<%@ 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>Insert title here</title>

</head>

<body>

<%

String name=config.getInitParameter("name");

out.print("Programming name is = "+name);

%>

</body>

</html>


Eclipse Code:

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"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

       version="3.0">

       <display-name>BasicPrograms</display-name>

       <welcome-file-list>

             <welcome-file>index.html</welcome-file>

             <welcome-file>index.htm</welcome-file>

             <welcome-file>index.jsp</welcome-file>

             <welcome-file>default.html</welcome-file>

             <welcome-file>default.htm</welcome-file>

             <welcome-file>default.jsp</welcome-file>

       </welcome-file-list>

 

       <servlet>

             <servlet-name>Home</servlet-name>

             <jsp-file>/configObject.jsp</jsp-file>

             <init-param>

                    <param-name>name</param-name>

                    <param-value>JavaTechSeries</param-value>

             </init-param>

       </servlet>

       <servlet-mapping>

             <servlet-name>Home</servlet-name>

             <url-pattern>/configObject.jsp</url-pattern>

       </servlet-mapping>

</web-app>

Here we are mentioning servlet mapping in web.xml to get called from jsp file. 
Output:
5. application Object:
JSP application implicit object is instance of javax.servlet.ServletContext implementation and it’s used to get the context information and attributes in JSP. We can use it to get the RequestDispatcher object in JSP to forward the request to another resource or to include the response from another resource in the JSP. 

Example of Application implicit object: 

Context.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>Insert title here</title>

</head>

<body>

<form action="Application.jsp">

<input type="submit" value="click to check context value"><br/>

</body>

</html>


Eclipse Code:

Application.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>Insert title here</title>

</head>

<body>

<%

String info=application.getInitParameter("info");

out.print("Program info = "+info); %>

</body>

</html>

Output:

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"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

       version="3.0">

       <display-name>BasicPrograms</display-name>

       <welcome-file-list>

             <welcome-file>index.html</welcome-file>

             <welcome-file>index.htm</welcome-file>

             <welcome-file>index.jsp</welcome-file>

             <welcome-file>default.html</welcome-file>

             <welcome-file>default.htm</welcome-file>

             <welcome-file>default.jsp</welcome-file>

       </welcome-file-list>

       <servlet-mapping>

             <servlet-name>One</servlet-name>

             <url-pattern>/Application.jsp</url-pattern>

       </servlet-mapping>

 

       <context-param>

             <param-name>info</param-name>

             <param-value>Example of application object</param-value>

       </context-param>

</web-app>


6. session object:
JSP session implicit object is instance of javax.servlet.http.HttpSession implementation. Whenever we request a JSP page, container automatically creates a session for the JSP in the service method. Since session management is a heavy process, so if we don’t want session to be created for JSP, we can use page directive to not create the session for JSP using <%@ page session="false" %>. This is very helpful when our login page or the index page is a JSP page and we don’t need any user session there 

Example of Session implicit object: 

<%@ 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>Insert title here</title>

</head>

<body>

<form action="Session1.jsp">

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

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

</form>

</body>

</html>


Eclipse Code:

Output:
Session1.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>Insert title here</title>

</head>

<body>

       <%

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

             out.print("Welcome " + name);

             session.setAttribute("user", name);

       %>

       <a href="session2.jsp">second jsp page</a>

</body>

</html>


Eclipse Code:
Output:
Session2.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>Insert title here</title>

</head>

<body>

       <%

             String name = (String) session.getAttribute("user");

             out.print("Hello " + name);

       %>

 

</body>

</html>



7. pageContext Object:

JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext abstract class implementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources. pageContext object also hold reference to other implicit object. 
The pageContext object can be used to set,get or remove attribute from one of the following scopes. 
• page
• request 
• session 
• application 
Example of pageContext implicit object

Input3.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>Insert title here</title>

</head>

<body>

<%

String name="JavaTechSeries";

pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);

pageContext.setAttribute("user",name,PageContext.PAGE_SCOPE);

pageContext.setAttribute("user",name,PageContext.REQUEST_SCOPE);

pageContext.setAttribute("user",name,PageContext.APPLICATION_SCOPE);

String sname=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);

String pname=(String)pageContext.getAttribute("user",PageContext.PAGE_SCOPE);

String rname=(String)pageContext.getAttribute("user",PageContext.REQUEST_SCOPE);

String aname=(String)pageContext.getAttribute("user",PageContext.APPLICATION_SCOPE);

%>

<b>Value in Session scope &nbsp;<%=sname%></b><br>

<b>Value in Request scope &nbsp;<%=rname%></b><br>

<b>Value in Page scope &nbsp;<%=pname%></b><br>

<b>Value in Application scope &nbsp;<%=aname%></b><br>

<a href="PageContext.jsp">One jsp page</a>

</body>

</html>


Eclipse Code:

PageContext.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>Insert title here</title>

</head>

<body>

<%

String sname=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);

String pname=(String)pageContext.getAttribute("user",PageContext.PAGE_SCOPE);

String rname=(String)pageContext.getAttribute("user",PageContext.REQUEST_SCOPE);

String aname=(String)pageContext.getAttribute("user",PageContext.APPLICATION_SCOPE);

%>

<b>Value in Session scope &nbsp;<%=sname%></b><br>

<b>Value in Request scope &nbsp;<%=rname%></b><br>

<b>Value in Page scope &nbsp;<%=pname%></b><br>

<b>Value in Application scope &nbsp;<%=aname%></b><br>

</body>

</html>


Eclipse Code:
Output:
8. page Object:

JSP page implicit object is instance of java.lang.Object class and represents the current JSP page. page object provide reference to the generated servlet class. This object is very rarely used. When a jsp is translated to an internal Servlet, we can find the following statement in the service() method of servlet. Object page=this; For using this object it must be cast to Servlet type. 

For  Example:

<% (HttpServlet)page.log("message"); %>


Since, it is of type Object it is less used because you can use this object directly in jsp. 

For example: 

<% this.log("message"); %>


9. exception Object:

JSP exception implicit object is instance of java.lang.Throwable class and used to provide exception details in JSP error pages. We can’t use this object in normal JSP pages and it’s available only in
JSP error pages. 
Example: This program is to show use of the <%=exception> object. In this 3 files are created. One file is used to take input from the user for the calculation which is “calculation.jsp”. And another file is used to perform the operation which is “Operation.jsp”. Third file is Exception file which is “exception.jsp”. For this 2 changes are required in jsp page directive. 

• In every file at page directive isErrorPage=”true”. 
• And set errorPage=”esception.jsp”.

Input.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>Insert title here</title>

</head>

<body>

<form action="Operation.jsp">

Input First Integer:<input type="text" name="firstnum" />

Input Second Integer:<input type="text" name="secondnum" />

<input type="submit" value="Get Results"/>

</form>

</body>

</html>


Operation.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">

<%@ page errorPage="exception.jsp" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%

String num1=request.getParameter("firstnum");

String num2=request.getParameter("secondnum");

int v1= Integer.parseInt(num1);

int v2= Integer.parseInt(num2);

int res= v1/v2;

out.print("Output is: "+ res);

%>

</body>

</html>


exception.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">

<%@ page isErrorPage="true" %> <html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Got this Exception: <%= exception %>

<b>Please enter correct value</b>

</body>

</html>




Recommended Topics





Previous
Next Post »