82 CHAPTER 2: Building Web Applications Using Servlets and JSP
Listing 2-20. Usage of Common Implicit Objects
1.getServletContext().setAttribute("book", book);
2.request.setAttribute("book", book);
3.request.getSession().setAttribute("book", book);
4.application.setAttribute("book" book);
5.request.setAttribute("book" book);
6.session.setAttribute("book" book);
7.pageContext.setAttribute("book" book);
Line 1: This sets the book attribute in a ServletContext without using implicit
objects.
Line 2: This sets the book attribute in a request object. The request object is
also an implicit object in JSP. Hence, setting the attribute in a servlet is similar to
setting the attribute in a JSP page.
Line 3: This sets the book attribute in the session without using implicit objects.
Line 4: This sets the book attribute in the ServletContext using the application
implicit object.
Line 5: This sets the book attribute in a request object. request is also an implicit
object in JSP. Hence, setting the attribute in JSP is similar to setting the attribute
in a servlet.
Line 6: This sets the book attribute in the session using a session implicit object.
Line 7: This sets the book attribute in PageContext using a pageContext implicit
object. There is no equivalent of pageContext in a servlet. A PageContext
instance provides access to all the namespaces associated with a JSP
page, provides access to several page attributes, and provides a layer above
the implementation details. Implicit objects are added to the pageContext
automatically.
Standard Actions
The JSP standard actions provide a way to do the following:
Manipulate JavaBeans
Dynamically include files
Perform URL forwarding
The
The
contents of a separate web component in the declaring JSP page. The syntax for using the standard
include action is as follows: