2. Agenda
• Understanding built In Objects
• Request, response
• Out
• session
• exception
• page and application scope
3. Implicit Object
• A JSP page has access to certain implicit objects that are always
available, without being declared first
• Created by container
• Corresponds to classes defined in Servlet
4. Implicit Object
- Provide access to many servlet capabilities within a JSP
- Four scopes
• Application scope
- Objects owned by the container application
- Any servlet or JSP can manipulate these objects
• Session scope
- Objects exist for duration of client’s browsing session
- Objects go out of scope when client terminates session or when
session timeout occurs
• Request scope
- Objects exist for duration of client request
- Objects go out of scope after response sent to client
• Page scope
- Objects that exist only in page in which they are defined
- Each page has its own instance of these objects
5. Implicit Object
Implicit object Description
Application Scope
application This javax.servlet.ServletContext object represents the container in which the JSP
executes.
Page Scope
config This javax.servlet.ServletConfig object represents the JSP configuration options. As
with servlets, configuration options can be specified in a Web application descriptor.
exception This java.lang.Throwable object represents the exception that is passed to the JSP error
page. This object is available only in a JSP error page.
out This javax.servlet.jsp.JspWriter object writes text as part of the response to a request.
This object is used implicitly with JSP expressions and actions that insert string content in a
response.
page This java.lang.Object object represents the this reference for the current JSP instance.
pageContext This javax.servlet.jsp.PageContext object hides the implementation details of the
underlying servlet and JSP container and provides JSP programmers with access to the implicit
objects discussed in this table.
response This object represents the response to the client and is normally an instance of a class that
implements HttpServletResponse (package javax.servlet.http). If a protocol other
than HTTP is used, this object is an instance of a class that implements
javax.servlet.ServletResponse.
Request Scope
request This object represents the client request. The object normally is an instance of a class that
implements HttpServletRequest (package javax.servlet.http). If a protocol other
than HTTP is used, this object is an instance of a subclass of javax.servlet.Servlet-
Request.
Session Scope
session This javax.servlet.http.HttpSession object represents the client session information
if such a session has been created. This object is available only in pages that participate in a
session.
6. Implicit Object and their classes
• request (HttpServletRequest)
• response (HttpServletRepsonse)
• session (HttpSession)
• application(ServletContext)
• out (of type JspWriter)
• config (ServletConfig)
• pageContext(this)
7. Creating an exception with an error page
• Determine the exception thrown
• In each of your JSP, include the name of the error page
- <%@ page errorPage="errorpage.jsp" %>
• Develop an error page, it should include
- <%@ page isErrorPage="true" %>
• In the error page, use the exception reference to display exception
information
<%= exception.toString() %>
8. About Exception Object
• Instance of java.lang.Throwable
• It is available in those pages where isError page directive is set to
true
• Important methods
- public String getlocalizedMessage()
- public String getMessage()
- public void printStackTrace()
- public void printStackTrace(PrintStream ps)
- public void printStackTrace(PrintWriter pw)
9. Session Object
• This denotes the data associated with a specific session of user.
• The class or the interface name of the object Session is
http.HttpSession.
• The previous two objects, request and response, are used to pass
information from web browser to server and from server to web
browser respectively.
• The Session Object provides the connection or association between
the client and the server.
• The main use of Session Objects is for maintaining states hen there
are multiple page requests.
10. Session Object
• It is an instanceof javax.servlet.http.HttpSession
• getAttributes returns the bounded object or null
• getAttributes return Enumeration of String Objects containing names
of all Object bounded to the session
• getCreationTime returns the time in millseconds when the session
• isNew() returns true if the server has created a session that hasn’t been
accessed by the client
• getId() returns String Object containing a unique identifier for his
session
• Invalidate() invalidates the session and unbinds any object bound to it
11. Request Object
• The request object is an instance of a
javax.servlet.http.HttpServletRequest object. Each time a client
requests a page the JSP engine creates a new object to represent
that request.
• The request object provides methods to get HTTP header
information including form data, cookies, HTTP methods etc.
12. Request Object methods
• public Object getAttribute(String name)
• getAttributeNames() returns an Enumeration containing the attribute names
available to the invoking ServletRequest object:
• public java.util.Enumeration getAttributeNames()
• getAuthType() returns the name of the authentication scheme used in the
request or null if no authentication scheme was used. It returns one of the
constants BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, or
DIGEST_AUTH, or it returns null if the request wasn't authenticated:
• public String getAuthType()
• getCharacterEncoding() returns a String object containing the character
encoding used in the body of the request or null if there's no encoding:
• public String getCharacterEncoding()
13. Request Object methods
• public Object getAttribute(String name)
• getAttributeNames() returns an Enumeration containing the attribute names
available to the invoking ServletRequest object:
• public java.util.Enumeration getAttributeNames()
• getAuthType() returns the name of the authentication scheme used in the
request or null if no authentication scheme was used. It returns one of the
constants BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, or
DIGEST_AUTH, or it returns null if the request wasn't authenticated:
• public String getAuthType()
• getCharacterEncoding() returns a String object containing the character
encoding used in the body of the request or null if there's no encoding:
• public String getCharacterEncoding()
14. Response Object
• The response object is an instance of a
javax.servlet.http.HttpServletResponse object. Just as the server
creates the request object, it also creates an object to represent the
response to the client.
• The response object also defines the interfaces that deal with
creating new HTTP headers. Through this object the JSP
programmer can add new cookies or date stamps, HTTP status
codes etc.
15. out Object
• The out implicit object is an instance of a javax.servlet.jsp.JspWriter
object and is used to send content in a response.
• The initial JspWriter object is instantiated differently depending on
whether the page is buffered or not. Buffering can be easily turned off
by using the buffered='false' attribute of the page directive.
• The JspWriter object contains most of the same methods as the
java.io.PrintWriter class. However, JspWriter has some additional
methods designed to deal with buffering. Unlike the PrintWriter object,
JspWriter throws IOExceptions.
16. application Object
• The application object is direct wrapper around the ServletContext
object for the generated Servlet and in reality an instance of a
javax.servlet.ServletContext object.
• 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.
17. config Object
• The config object is an instantiation of javax.servlet.ServletConfig and
is a direct wrapper around the ServletConfig object for the generated
servlet.
• This object allows the JSP programmer access to the Servlet or JSP
engine initialization parameters such as the paths or file locations etc.
18. pageContext Object
• The pageContext object is an instance of a javax.servlet.jsp.PageContext object. The
pageContext object is used to represent the entire JSP page.
• 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