JSP Tutorial - JSP Implicit Objects

JSP Implicit Objects are the Java objects available in each page and can be used directly without explicitly declaration.

The following table lists nine Implicit Objects.

Object Description
request HttpServletRequest object associated with the request. Each time a client requests a page the JSP engine creates a new object to represent that request. We can use the request object to get HTTP header information such as form data, cookies, HTTP methods etc.
response HttpServletResponse object associated with the response to the client. JSP engine creates an object to represent the response to the client. We can use this object to add new cookies or date stamps, HTTP status codes etc to the response.
out PrintWriter object used to send output to the client. We can use buffer in the output by using the buffered='false' attribute of the page directive. out.print(dataType dt) prints a data type value. out.println(dataType dt) prints a data type value then terminate the line with new line character. out.flush() flushes the stream.
session HttpSession object associated with the request. The session object tracks client session between client requests.
application ServletContext object associated with application context. This object reoresents the JSP page through the entire lifecycle. It is created when the JSP page is initialized and will be removed when the JSP page is removed by the jspDestroy() method.
config ServletConfig object associated with the page. We can use this object to access the Servlet or JSP engine initialization parameters such as the paths or file locations etc. config.getServletName() returns the servlet name , which is the string contained in the <servlet-name> element defined in the WEB-INF\web.xml file.
pageContext The pageContext object is an instance of a javax.servlet.jsp.PageContext object, which is used to represent the entire JSP page. This object contains informations about the request and response objects for each request. We can also get the directives issued to the JSP page, including the buffering information, the errorPageURL, and page scope from this object. The PageContext class defines fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE to identify the four scopes.
page A synonym for this, and is used to call the methods defined by the translated servlet class.
Exception Exception object used to access the exception data by JSP.

JSP Hits Counter

To implement a hit counter, use getAttribute() and setAttribute() from Application implicit object, which is a representation of the JSP page.

The following code shows the syntax to set/get a variable at application level:

application.setAttribute(String Key, Object Value);
application.getAttribute(String Key);

The following code shows how to count total number of hits on a particular page.

<%@ page import="java.io.*,java.util.*" %>

    Integer hitsCount = (Integer)application.getAttribute("hitCounter");
    if( hitsCount ==null || hitsCount == 0 ){
       out.println("first time!");
       hitsCount = 1;
       /* return visit */
       out.println("Welcome back!");
       hitsCount += 1;
    application.setAttribute("hitCounter", hitsCount);
<p>Total number of visits: <%= hitsCount%></p>