Nearby lessons
25 of 30JSP Examples - Implicit Objects & Scopes
- Use every implicit object in a real JSP
- Configure error pages two ways
- Manage attributes across page/request/session/application scopes
Demo programs for the nine JSP implicit objects (request, response, out, session, application, config, page, pageContext, exception), error-page configuration, attribute scopes and JSP Documents.
1) Request and Response Implicit Objects
2) Application Implicit Object
3) Session Implicit Object
If we can make session implicit object unavailable by using page directive then we are not allowed
to use session implicit object. otherwise we will get compile time error.
test.jsp:
3) Session Implicit Object
CE: session cannot be resolved.
Q. In which of the following cases session object available in the JSP?
3) Session Implicit Object
4) Config Implicit Object
config.jsp:
4) Config Implicit Object
2) The Init Param value is
The Logical Name: DemoJSP
The Init Param value is : UttarPradesh
http://localhost:7777/jsp1/config.jsp
The Logical Name: jsp
The Init Param value is : null
Note: To reflect Servlet level web.xml configurations in the JSP, compulsory we should access by
using url-pattern.
2) The Init Param value is
The pageContext implicit object is of type javax.servlet.jsp.PageContext.
It is abstract class and web server vendor is responsible to provide implementation.
PageContext is the child class of JspContext.
| JspContext | Simple Custom Tag (JSP 2.0 V) |
|---|
PageContext Classic Custom Tag (JSP 1.1 V)
We can use pageContext implicit object for the following 3 purposes.
2) The Init Param value is
a) Getting JSP Implicit objects from pageContext
These methods are not useful within the JSP. We can use these methods outside of JSP, mostly in
Custom Tag Handlers.
b) Request Dispatching by using pageContext
The target resource can be specified by either relative path or absolute path.
Eg:
first.jsp:
b) Request Dispatching by using pageContext
<h1>Hello This is Second JSP</h1>
forward:
o/p: Hello This is Second JSP
include:
o/p: Hello This is First JSP
Hello This is Second JSP
c) Attribute Management in any scope
This implicit object is of type javax.servlet.jsp.JspWriter.
This class is specially designed for JSPs to write character data to the response.
The main difference b/w JspWriter and PrintWriter is, in PrintWriter buffering concept is not
available. Hence if we are writing anything by using PrintWriter, it will write directly to the
response object.
But in the case of JspWriter, buffer concept is available. Hence if we are writing anything by
using JspWriter, first it will write to buffer and at least total buffered data will write to
response.
Except this buffer difference there is no other difference b/w JspWriter and PrintWriter.
JspWriter = PrintWriter+Buffer
PrintWriter
Response
Object
JspWriter
Buffer
test.jsp:
c) Attribute Management in any scope
o/p:
Sunny
Sunny
Sunny
Mallika
Mallika
Mallika
Note: Within the JSP we can use either PrintWriter or JspWriter but not recommended to use
both simultaneously.
Note:
JspWriter is the child class of Writer(AC).Hence all methods of Writer class are by default
available to the JspWriter through inheritance.
In addition to these methods, JspWriter contains its own set of print() and println() to add any
type of Data to the response.
| Writer (AC) | write (int ch) |
|---|
write (char[] ch)
write (String s)
close()
flush()
| JspWriter (AC) | print() |
|---|
println()
test.jsp:
c) Attribute Management in any scope
Q1. In JSP, How we can write Response?
By using out implicit object, which is of the type : JspWriter
Q2. What is the difference b/w PrintWriter and JspWriter?
Buffer
JspWriter = PrintWriter+Buffer;
7) Page Implicit Object
Note: On the page variable we cannot call servlet specific methods. Hence page implicit object is
the most rarely used object in the JSP.
8) Configuring Error Pages in the JSP (Exception Implicit Object)
1) Declarative Approach
The error pages configured in this approach are applicable to the entire web application.
2) Programmatic Approach
In demo.jsp if any exception or error occurs then error.jsp is responsible to report this error.
This way of configuring error page is applicable for a particular JSP.
http://localhost:7777/jsp1/demo.jsp
We can declare a JSP as error page by using isErrorPage attribute of page directive.
In the error pages only exception implicit object is available.
error.jsp:
2) Programmatic Approach
If JSP is not declared as error page but if we are trying to access exception implicit object then
we will get compile time error saying-"exception cannot be resolved"
Note: If we are sending the request to error page directly without any exception, then
exception implicit object refers "null"
Which approach is recommended?
Declarative Approach is recommended b'z we can customize error pages based on exception type
and error code.
Note:
- All JSP Implicit objects are available as local variables of _jspService() method in the Generated
Servlet. Hence inside _jspService() method only we can access implicit objects. But outside of
_jspService() method we cannot access.
Scriptlet and expression tags code will be placed inside _jspService() method. Hence inside
scriptlet and expression tags we can use jsp implicit objects.
But declaration tag code will be placed outside of _jspService() method in the generated servlet.
Hence we cannot use jsp implicit objects inside declaration tag.
Eg 1: <%
out.println("Hello");
%>
Eg 2: <%!
public void m1()
{
out.println("Hello");
}
%>
Eg 3: Session Id: <%= session.getId() %>
- Among all JSP implicit objects except session and exception, all remaining objects always
available in every JSP. We cannot make these objects unavailable.
session object by default available in every JSP. But we can make it unavailable by using page
directive as follows..
<%@ page session="false" %>
exception implicit object is by default not available in every JSP. We can make it avialble by using
page directive as follows..
<%@ page isErrorPage="true" %>
JSP Scopes
In Servlets we have the following 3 scopes for storing information in the form of attributes...
- Request Scope
- Session Scope
- Application Scope
In addition to these 3 scopes in JSPs, we have page Scope also...
- Request Scope:
In Servlets this scope is maintained by ServletRequest object but in JSPs, it is maintained by
request implicit object.
The information stored in request scope is available for all the components which are processing
that request.
Request scope will start at the time of request object creation(i.e. just before calling service()
method) and ends at the time of request object destruction(i.e. just after completing service()
method).
Once request processing completed, request scoped attributes will be destroyed.
We can perform attribute management in request scope by using the following methods...
public void setAttribute(String name,Object value)
public Object getAttribute(String name)
public void removeAttribute(String name)
public Enumeration getAttributeNames()
- Session Scope:
In Servlets session scope is maintained by HttpSession object, but in JSPs session scope is
maintained by session implicit object.
Session scope will be started at the time of session object creation and ends once session expires
either by logout mechanism(invalidate() method) or by timeout mechanism.
The information stored in session scope is available for all components which are participating in
that session.
HttpSession contains the following methods for attribute management in session scope
public void setAttribute(String name,Object value)
public Object getAttribute(String name)
public void removeAttribute(String name)
public Enumeration getAttributeNames()
- Application Scope:
In Servlets this scope is maintained by ServletContext object, but in JSPs this scope is maintained
by application implicit object.
The information stored in application scope is available for all components of the web application.
Application scope will be started at the time of ServletContext object creation (i.e. at the time of
application deployment or at server startup) and will be ends at the time of context object
destruction(ie at the time of application undeployment or server shutdown)
ServletContext interface defines the following methods for attribute management in application
scope.
public void setAttribute(String name,Object value)
public Object getAttribute(String name)
public void removeAttribute(String name)
public Enumeration getAttributeNames()
Q. Write a JSP to print hit count of the application?
hitcount.jsp:
2) Programmatic Approach
Q. Write a JSP to print number of users login into our application?
sessioncount.jsp:
2) Programmatic Approach
Q. Write a JSP to display the number of requests in the current session?
sessionreqcount.jsp:
2) Programmatic Approach
- Page Scope:
This scope is applicable only for JSPs but not for Servlets.
This scope is maintained by pageContext implicit object.
The information stored in Page Scope is available only in the current JSP and not available for
other JSPs.Hence it is most restricted scope.
Page Scope is the most commonly used scope in custom tags to share information between tag
handler classes.
PageContext class defines the following methods for attribute management in page scope.
2) Programmatic Approach
Extra methods of PageContext class to perform attribute management in any scope:
PageContext class defines the following methods to perform attribute management in any scope...
2) Programmatic Approach
The allowed scopes are:
PageContext.PAGE_SCOPE 1
PageContext.REQUEST_SCOPE 2
PageContext.SESSION_SCOPE 3
PageContext.APPLICATION_SCOPE 4
2) Programmatic Approach
Q.Which of the following is valid way to store an attribute in Page Scope?
2) Programmatic Approach
PageContext class defines the following extra methods also..
2) Programmatic Approach
First it will searches in page scope and if the attribute present then returns its value. If it is not
available then it will search in request, session and then application scopes respectively.
This method acts as all rounder.
Q. What is the difference b/w getAttribute(String name) and findAttribute(String name)?
Q. What is the difference b/w getAttribute(String name) and getAttribute(String name,int scope)?
Demo Program to demonstrate JSP Scopes
Demo Program to demonstrate findAttribute()method
There are 2 Types of syntaxes are possible to write JSPs.
Demo Program to demonstrate findAttribute()method
If we are developing JSPs by using JSP Standard syntax, such type of JSPs are called JSP Pages.
If we are developing JSPs by using XML Based syntax, such type of JSPs are called JSP Documents.
4) Directives
In XML: <jsp:directive.page import="java.util.*" />
4) Directives
In XML: <jsp:directive.include file="header.jsp" />
For the taglib directive there is no equivalent syntax in XML.We can provide equivalent syntax
indirectly by using <jsp:root> tag.
....
</jsp:root>
7) Template Text
7) Template Text
Note: From Servlet 2.4 Version onwards Web container can identify JSP Document automatically.
No special arrangement is required.
http://localhost:7777/jsp2/jspdoc1.jsp
Write a JSP Document to print hit count of the JSP.
JSP Page
7) The Hit count is
JSP Document
Note:
It is not recommended to use both standard and xml based syntaxes simultaneously.
The main advantage of XML Based syntax is ,we can use xml editors like XML Spy for
writing and debugging JSPs very easily.
Note: In XML Based syntax all tags,attributes are case sensitive. Attribute values must be enclosed
either in single quotes or in double quotes.
Q. Which of the following is the valid way of importing java.util package in the JSP?
<jsp:page import="java.util.*" />
<jsp:directive page import="java.util.*" />
<jsp:page.directive import="java.util.*" />
<jsp:directive.page import="java.util.*" />
- out is a JspWriter (buffered); pageContext is the gateway to all scopes
- exception is only available on isErrorPage="true" pages
- findAttribute() searches page, request, session, application in order