JSP Implicit Objects
In the previous sections, we have examined the generated JSP Servlet source code, we know that the code contains several object declarations in its _jspService() method. Recall this part from the code in the preceding section: we see that there are object references, such as pageContext, session, application, config, out, and so on. These object references are created whether they are used from inside the page. They are automatically available for the JSP page author to use. These objects are called implicit objects and are summarized in the Table 1.
Table 1. JSP Implicit Objects
Object | Type | Description |
out | javax.servlet.jsp.JspWriter | Refers to the output stream for the page |
request | javax.servlet.http.HttpServletRequest | Refers to the current request to the page |
response | javax.servlet.http.HttpServletResponse | Used for sending a response to the client |
session | javax.servlet.http.HttpSession | Refers to the user’s session |
config | javax.servlet.ServletConfig | Refers to the Servlet’s configuration |
application | javax.servlet.ServletContext | Refers to web application’s environment |
page | javax.servlet.jsp.HttpJspPage | Refers to the page’s servlet instance |
pageContext | javax.servlet.jsp.PageContext | Refers to the page’s environment |
exception | java.lang.Throwable | Used for error handling purposes |
Example Codes for JSP Implicit Objects
All the implicit objects are discussed briefly in the following section with JSP coding examples. The example JSP codes are placed within JSPImplictObjects directory under webapps.
1) out
The implicit object out is probably the most frequently used implicit object. We call either its print method or its println method to send text or other data to the client browser. In a servlet, we always need to call the getWriter() method of the javax.servlet.http.HttpServletResponse interface to obtain a PrintWriter before we can output anything to the browser, as follows:
PrintWriter out = response.getWriter();
In JSP, we don’t need to do this because we already have an out that represents a javax.servlet.jsp.JspWriter object.
2) request and 3) response
In Servlets, both request and response objects are passed in by the Servlet container to the service() method of the javax.servlet.http.HttpServlet class. Its signature is as follows:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
In a Servlet, before we send any output, we are required to call the setContentType() method of the HttpServletResponse to tell the browser the type of the content, as in the following code:
response.setContentType("text/html");
In JSP, this is done automatically for us in the generated JSP servlet class.
Having an request object of javax.servlet.http.HttpServletRequest class and an response object of javax.servlet.http.HttpServletResponse class, we can do anything we like as we would in a Servlet.
Coding Example 1
The following codes demonstrates the use of out, request and response objects:
index.html
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="greetings.jsp">
<b>Enter your name: </b>
<input type="text" name="name">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
greetings.jsp
<html>
<head>
<title>Greetings Page</title>
</head>
<body>
<%
//use of 'request' object
String name = request.getParameter("name");
//use of 'out' object
out.println("Welcome " + name + ". Have a nice day!" + "<br>");
%>
<form action="redirect.jsp">
Click this for redirection to another page
<input type="submit" value="Go">
</form>
</body>
</html>
redirect.jsp
<html>
<body>
<%
//use of 'response' object
response.sendRedirect("http://www.techguruspeaks.com");
%>
</body>
</html>
Output:
When the “Submit” button is clicked, the control will be transferred to greetings.jsp file with the HTML form data. See it below.
Now, if the “Go” button is clicked, the control will be redirected to the mentioned URL (http://www.techguruspeaks.com) as described in redirect.jsp file.
4) session
In JSP we have been provided an implicit object session so we don’t need to create an object of session explicitly as we do in Servlets. In JSP the session is by default true. The session is defined inside the directive <%@ page session=”true/false” %>. If we don’t declare it inside the JSP page then session will be available to the page, as it is default by true. This session object is of type javax.servlet.http.HttpSession class.
We can tell the container to disable session in the JSP file by setting the session attribute to false. Set the session attribute of the page directive to false, as shown in the following example:
<%@ page session="false" %>
The session implicit object represents the HttpSession object that we can retrieve in a Servlet by calling the getSession() method of the well-known interface javax.servlet.http.HttpServletRequest, as in the following code:
request.getSession();
Coding Example 2
The following codes demonstrates the use of session object:
start.html
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="session.jsp">
<b>Enter your name: </b>
<input type="text" name="name">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
session.jsp
<html>
<body>
<%
String name = request.getParameter("name");
out.println("Hello " + name + "!" + "<br>");
//use of 'session' object
out.println("Session Id: " + session.getId() + "<br>");
session.setAttribute("user", name);
%>
<br>
<a href="other.jsp">Other JSP Page</a>
</body>
</html>
other.jsp
<html>
<body>
<%
String name = (String)session.getAttribute("user");
out.println("Session Id: " + session.getId() + "<br>");
out.println("Bye " + name + "!" + "<br>");
%>
</body>
</html>
Output:
When the “Submit” button is clicked, the control will be transferred to session.jsp file with the HTML form data.
If the hyperlink named “Other JSP Page” is clicked, the control will be transferred to the other.jsp file.
Note: In the last two cases shown above, we can see that the session id is same.
5) config
The config implicit object represents a javax.servlet.ServletConfig object that in a Servlet can be retrieved by using the getServletConfig() method.
6) application
The application implicit object represents the javax.servlet.ServletContext object. In an HttpServlet, we can retrieve the ServletContext method by using the getServletContext() method.
Coding Example 3
The following codes demonstrates the use of config and application objects using a deployment descriptor (web.xml):
web.xml
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<jsp-file>/testing.jsp</jsp-file>
<init-param>
<param-name>City</param-name>
<param-value>Delhi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/testing</url-pattern>
</servlet-mapping>
<context-param>
<param-name>Country</param-name>
<param-value>India</param-value>
</context-param>
</web-app>
input.html
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="testing">
<b>Enter your name: </b>
<input type="text" name="name">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
testing.jsp
<html>
<head>
<title>Config & Application</title>
</head>
<body>
<%
String name = request.getParameter("name");
out.println("Welcome " + name + "!" + "<br>");
//use of 'config' object
String city = config.getInitParameter("City");
out.println("City = " + city + "<br>");
//use of 'application' object
String country = application.getInitParameter("Country");
out.println("Country = " + country + "<br>");
%>
</body>
</html>
Output:
If the “Submit” button is clicked, the control will be transferred to testing.jsp file with the HTML form data. See below.
7) page
The page implicit object represents the javax.servlet.jsp.HttpJspPage interface.
8) pageContext
The pageContext implicit object represents the javax.servlet.jsp.PageContext object.
Coding Example 4
The following code demonstrates the use of page and pageContext objects:
test.jsp
<html>
<body>
<%
//testing of 'page' object
String page_name = page.toString();
out.println("Page Name is: " + page_name + "<br>");
//testing of 'pageContext' object
pageContext.setAttribute("guru", "techguru", pageContext.PAGE_SCOPE);
String name = (String)pageContext.getAttribute("guru");
out.println("Guru name is: " + name);
%>
</body>
</html>
Output:
9) exception
The exception object is available only on pages that have been defined as error pages.
This concept will be discussed in the topic named JSP Exception Handling.