Saturday, April 28, 2012

What are implicit objects

Implicit objects are Instances of some of the Servlet API classes, which are available to all the JSP pages to be used within Scriptlets and expressions. You don't need to instantiate them, they are available Implicitly, and hence the name Implicit Objects. Implicit objects has standard variable names and are available in all JSP without writing any extra code.
If developer's does not instantiate implicit objects then who does it ! The answer is, JSP container. It is the responsibility of container to instantiate Implicit objects and make them available to JSP pages.

What implicit objects are used for !

Depending upon the type of the object, an Implicit object can be used to do various things. For example, request implicit object can be used to obtain request parameters, out implicit object can be used to write to the response. application implicit object can be used to obtain the ServletContext attributes.
Following is a list of all JSP implicit objects.
  1. request
  2. response
  3. out
  4. pageContext
  5. session
  6. application
  7. config
  8. page
  9. exception

JSP implicit objects

Now let's look at each of the Implicit objects, which type of object it is and some examples of how it can be used.

The request object

Variable name : request
Type : javax.servlet.http.HttpServletRequest
The request object provides acceess to information like HTTP headers, request parameters, and request attributes.
Example
Print value of a named request parameter using expression
<%= request.getParameter("name") %>

Access the value of a request attribute of type String inside scriptlet.
<% 
 String name = request.getAttribute("name");
%>

The response object

Variable name : response
Type : javax.servlet.http.HttpServletResponse
The response object is an instance of HttpServletResponse. The response object can be used for things like setting a response header and sending a cookie.
Example
Set a cache control header.
<% 
response.setHeader("Cache-Control”, "no-cache");
%>

The pageContext object

Variable name : pageContext
Type : javax.servlet.jsp.PageContext
The pagecontext object provides access to page attributes, It is the convinient place to put the shared data within the page.
Example
set an attibute in pagecontext
<% 
pagecontext.setAttribute("name", "servletworld");
%>
Access value of a pagecontext attribute and output to response.
<%= pagecontext.getAttribute("name") %>

The session object

Variable name : session
Type: javax.servlet.http.HttpSession
The session object represents session created for a client and can be used to store and retrive data from session. The session object is not available if the session="false" is used in page directive.
Example
Store username as session attribute.
<% 
session.setAttribute("username", "servletworld");
%>

output the value of a session attribute.
<%=  session.getAttribute("username") %>

The application object

Variable name : application
Type : avax.servlet.ServletContext
application object is an instance of ServletContext, It can be used to obtain the context parameters, or get/set context attributes.
Example
Assign value of a context init parameter to a variable.
<%
 String param1 = application.getInitParameter("param1");
 %>

The out object

variable name : out
Type : javax.servlet.jsp.JspWriter
The out object is an instance of JspWriter that can be used to write to the response.
Example
Write the value of a session attribute to response.
<%
out.write(session.getAttribute("username"));
 %>

The config object

Variable name : config
Type : javax.servlet.ServletConfig
The config objet can be used to access the page configuration.
Example
Get the value of a configuration parameter.
<% 
String param1 = config.getInitParameter("param1");
%>

The page object

Variable name : page
Type : java.lang.Object
The page object is an instance of the page's implementation servlet class. The page object can be thought of as a synonym to the 'this' keyword withi page.

The exception object

Variable name : exception
Type : java.lang.Throwable
The exception object is available within the error pages (The page with isErrorPage="true"). It represents the uncaught exception which caused the exception page to be invoked.

No comments:

Post a Comment