JSP Implicit Objects
JSP Implicit Objects
JSP Implicit Objects
JSP Implicit Objects are the Java objects that the JSP Container makes available to
developers in each page and developer can call them directly without being explicitly
declared. JSP Implicit Objects are also called pre-defined variables.
Object Description
It refers HttpServletResponse object associated
Response
with the response to the client.
object.
2. Whenever client makes a requests to the JSP page, JSP engine creates a new
3. The request object have methods by which we can get HTTP header information.
4. HTTP header information includes form data, cookies, HTTP methods etc.
<%
String name = request.getParameter("name");
out.println("Hello " + name);
%>
For the URL http://127.0.0.1:8080/net/getParameterDemo.jsp?name=Monali, it displays the following:
1. Hello Monali
object.
3. The response object also defines the interfaces that deal with creating new HTTP
headers.
4. Through this object the JSP programmer can add new cookies or date stamps,
3. Buffering can be easily turned off by using the buffered=’false’ attribute of the
page directive.
designed to deal with buffering. Unlike the PrintWriter object, JspWriter throws
IOExceptions.
Following are the important methods which we would use to write boolean char, int,
double, object, String etc.
Method Description
The session object is used to track client session between client requests. We would
see complete usage of session object in coming chapter: JSP – Session Tracking.
This object is a representation of the JSP page through its entire lifecycle. This object is
created when the JSP page is initialized and will be removed when the JSP page is
removed by the jspDestroy() method.
By adding an attribute to application, you can ensure that all JSP files that make up
your web application have access to it.
You can check a simple use of Application Object in chapter: JSP – Hits Counter
This object allows the JSP programmer access to the Servlet or JSP engine initialization
parameters such as the paths or file locations etc.
The following config method is the only one you might ever use, and its usage is trivial:
config.getServletName();
This returns the servlet name, which is the string contained in the <servlet-name>
element defined in the WEB-INF\web.xml file
This object is intended as a means to access information about the page while avoiding
most of the implementation details.
This object stores references to the request and response objects for each request. The
application, config, session, and out objects are derived by accessing attributes of this
object.
The pageContext object also contains information about the directives issued to the JSP
page, including the buffering information, the errorPageURL, and page scope.
The PageContext class defines several fields, including PAGE_SCOPE, REQUEST_SCOPE,
SESSION_SCOPE, and APPLICATION_SCOPE, which identify the four scopes. It also
supports more than 40 methods, about half of which are inherited from the
javax.servlet.jsp.
JspContext class.
One of the important methods is removeAttribute, which accepts either one or two
arguments. For example, pageContext.removeAttribute (“attrName”) removes the
attribute from all scopes, while the following code only removes it from the page scope:
pageContext.removeAttribute("attrName", PAGE_SCOPE);
You can check a very good usage of pageContext in coming chapter: JSP – File
Uploading.
This object is an actual reference to the instance of the page. It can be thought of as an
object that represents the entire JSP page.
The exception object is a wrapper containing the exception thrown from the previous
page. It is typically used to generate an appropriate response to the error condition.
Browser sends lot of information along with the request. Information sent by browser
is stored in the request header of the HTTP request made by browser or any client side
program.
object.
2. Whenever client makes a requests to the JSP page, JSP engine creates a new
3. The request object have methods by which we can get HTTP header information.
4. HTTP header information includes form data, cookies, HTTP methods etc.
<table border="1">
<tr>
<th>Header Name</th><th>Header Value(s)</th>
</tr>
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
</body>
</html>
Output :
Header
Header Value(s)
Name
host localhost:8080
connectio
keep-alive
n
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.
accept
8
user- Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like
agent Gecko) Chrome/36.0.1985.125 Safari/537.36
accept-
gzip,deflate,sdch
encoding
accept-
en-US,en;q=0.8,hi;q=0.6,mr;q=0.4
language
Explanation :
1. Suppose we need to refresh the web page automatically after some time then
2. We need auto refresh of page while displaying the Cricket Score or Stock Market
Status
seconds.
Syntax :
Above method is used to send header “Refresh” to the browser. Integer value indicates
time interval in seconds.
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<h2>Auto Refresh Header Example</h2>
<%
// Page will be auto refresh after 1 seconds
response.setIntHeader("Refresh", 1);
Output :
Concept of Cookies :
4. Cookies are created when you use your browser to visit a website that uses
Types of Cookies :
Type of
Explanation
cookie
1. Server script sends a set of cookies to the browser when user visits site for first
3. When user re-visit the website next time, browser sends those cookies
information to the server and server uses that information to identify the user
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2014 11:01:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=pritesh; expires=Tuesday, 07-Oct-14 12:33:31 GMT;
path=/; domain=c4learn.com
Connection: close
Content-Type: text/html
You can see the name-value pair in the cookie file.Cookie file contain GMT date, a path
and a domain and expiration date.
Below is the example of the HTTP header that we send when user revisit the website.
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz
JSP Implicit Objects
JSP provides standard or predefined implicit objects, which can use directly in JSP page
usingJSP Scriptlet. The implicit objects are Servlet API class type and created by JSP
containers
There are 9 jsp implicit objects. These objects are created by the web container (JSP
The available implicit objects are out, request, response, page, pageContext, exception,
1 HttpServletRequest request
2 HttpServletResponse response
3 ServletConfig config
4 ServletContex application
5 HttpSession session
6 JspWriter/PrintWriter out
7 Exception exception
8 PageContext pagecontext
9 Object page
All implicit objects of jsp are accessible with in the expression and scriptlet of the jsp, but not
depends on session attribute of the page directive and exception depends on isErrorPage
The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp
request by the web container. It can be used to get request information such as parameter,
header information, remote address, server name, server port, content type, character
encoding etc.
It can also be used to set, get and remove attributes from the jsp request scope.
Example of request implicit object where we are printing the name of the user with welcome
message.
index.html
Example
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
Example
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
Example of response implicit object where we are redirecting the response to the Google.
index.html
Example
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
Example
<%
response.sendRedirect("http://www.google.com");
%>
whenever servlet object is created. This object can be used to get initialization parameter for
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
config object is accessible, only if the request is given to the jsp by using its url pattern, but
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
web.xml file
<web-app>
<servlet>
<servlet-name>Home</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("driver name is="+driver);
%>
In JSP, page is an implicit object of type Object class. When a jsp is translated to an internal
Servlet, we can find the following statement in the service() method of servlet.
Object page=this;
Example
example:
Example
In JSP, session is an implicit object of type HttpSession.This object used to set,get or remove
index.html
Example
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
Example
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
One.jsp
Example
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
JSP, exception is an implicit object of type java.lang.Throwable class. This object can be used
index.html
Example
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
Example
<%
response.sendRedirect("http://www.google.com");
%>
For all jsp in a web application, there must be a single application object with application
object we can share the data from one JSP to any other JSP in the web application.
The instance of ServletContext is created only once by the web container when application
This object can be used to get initialization parameter from configuration file (web.xml). It
can also be used to get, set or remove attribute from the application scope.
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>
web.xml
<web-app>
<servlet>
<servlet-name>One</servlet-name>
<jsp-file>/welcome.jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>One</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>
can be used to set,get or remove attribute from one of the following scopes.
page
request
session
application
index.html
Example
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
Example
<%
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
%>
One.jsp
Example
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
out.print("Hello "+name);
The directive elements are used to do page related operation during page translation time.
Page directive
Include directive
Taglib directive
Syntax of directive elements
Syntax
Page Directive
The page directive defines attributes that apply to an entire JSP page. This element is used to
import
contentType
extends
language
buffer
info
isELIgnored
isThreadSafe
autoFlush
session
pageEncoding
errorPage
isErrorPage
It is used to include some other pages into the JSP document, It may be JSP file, html file or
text file. This is known as static include because the target page is located and included at
The jsp page is translated only once so it will be better to include static resource.
Code Re-usability
Syntax
Example
<html>
<body>
</body>
</html>
Taglib Directive
This is used to use custom tags in JSP page, here the custom tag may be user defined ot JSTL
Syntax
<@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
uri
<html>
<body>
<mytag:currentDate/>
</body>
</html>
Action elements are used to performs specific operation using JSP container, like setting
values into java class, getting values from java class. The JSP action elements classified into
Standard Action are given in JSP to separate the presentation logic and the business logic of
Each Standard Action follows xml syntax, we do not have any equivalent html syntax.
<jsp: include>
<jsp: forward>
<jsp: param>
<jsp: params>
<jsp: plugin>
<jsp: useBean>
<jsp: setProperty>
<jsp: getProperty>
<jsp: fallback>