TomRed.net

  • Increase font size
  • Default font size
  • Decrease font size
Subscribe Bookmark and Share
Home Tutorials JavaScript Java JSP Implicit Objects

Java JSP Implicit Objects

User Rating: / 0
PoorBest 

In a JSP the web container provides 9 objects (for free) implicitly. These objects are directly available to the developer for use without instantiation etc. JSP provides certain Implicit Objects listed below. These implicit objects are generated by PageContext.

Object Type
application ServletContext
config ServletConfig
out JSPWriter
page Object
pageContext PageContext
request HttpServletRequest
response HttpServletResponse
session HttpSession

Application

This object belongs to class ServletContext and used to maintain certain information throughout the scope of Application. This offers methods for getting/setting attributes in application scope accessing init parameters and init parameter names.

application.getServletInfo();
application.getNameDispatcher();
application.getRealPath();
application.log("some log text");
application.getContextPath();
application.getAttributeNames();

Config

This offers methods to access the servlet name and also provides access to init parameters. Config methods include:

config.getInitParameterNames();
config.getServletName();

Out

This object is instantiated implicitly from JSP Writer class and can be used for displaying anything within delimiters. For e.g. out.println(“Hello World”); This is can be used to output template text although it is simpler to enter the template text directly in the JSP outside of scriptlet tags . Example of out methods include:

out.write("something interest");
out.close();
out.flush();
out.println("something else");

PageContext

This object is of class pageContext and is utilized to access the other implicit objects. This can provide direct access to the namespaces associated with a JSP page and provide access to several page attributes.

Request

It is an implicit object of class HttpServletRequest class and using this object request parameters and attributes can be accessed. You can access cookies from the request and the HTTP headers sent it in. Some common request methods are listed below.

request.getHeaderNames();
request.getCookie();
request.getMethod();
request.getParameter();
request.isRequestedSessionIdFromCookie();

Response

It is an implicit object of class HttpServletResponse class and using this object response(sent back to browser) parameters can be modified or set. Using the response you can gain access to the response mime type or set status codes among many other methods. Some examples are:

response.SC_FORBIDDEN;
response.addCookie(Cookie aCookie);
response.encodeURL("http://www.tomred.net");
response.sendRedirect("http://www.tomred.net");
response.setContentType("text/html");

Session

Session object is of class HttpSession and used to maintain the session information. It stores the information in Name-Value pair. Session has a number of useful methods available to it, it is a especially useful object providing mean to operate on a session and extract valuable information from it. Commonly used methods include:

session.getId();
session.invalidate();
session.isNew();
session.getMaxInactiveInterval();