Saturday, August 23, 2014

Implicit variables and objects in JSP

This is another important lesson in JSP. Before start this section it is better if you can look at our previous examples that have been done using JSP. In  this examples we used some variables without declaration and initialization (out,page...etc). JSP engine makes nine implicit variables to  the JSP page.

The objects that these variables are refer to are called implicit objects.

application variable and object

This variable is belongs to javax.servlet.ServletContext class. This refers to the environment of the web application. 

The application object is a representation of the JSP page through its entire life circle. IT is created when the JSP page is initialized and it will be removed by jspDestroy() method.

<% out.print(application.getServerInfo()); %>

session variable and object

You have learnt a attribute called session in JSP directives. This implicit variable is declared only if the session attribute  of the page directive is true. If session="false", JSP engine doesn't declare this variable. The session object is a instance of javax.servlet.http.HttpSession interface. This is used to track client sessions.

<%@page session="true" %>

<% out.print(session.getId());  %>

request / responce variable and object

These two implicit variables are related to javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponce interfaces. The JSP engine creates new request objects for each and every client requests and also creates response objects to response clients. There are so many method associated with request and response objects.

    String filePath = request.getServletPath();
    out.print("Your file path is: " + filePath);

config variable and object

config variable is a type of javax.servlet.ServletConfig. This object allows to programmer to access to parameters of the JSP engine such as path, location, file name...etc.

<% out.print(config.getServletContext()); %>

out variable and object

This is a instance of javax.servlet.jsp.JspWriter class. This object has similar methods as PrintWriter class in Java. But additionally it has some methods to deal with buffering. This can be used directly in scriptlets and indirectly in expressions.  

<% out.print("Hi one"); %>

<%= "Hi two" %>

page variable and object

This is a instance of java.lang.Object class. This is entirely talk about the whole page and used to call the methods defined by the translated servlet class.

<% out.print(page.getClass().toString()); %>

pageContext variable and object

This object is also use to represent entire JSP page and it is a instance of javax.servlet.jsp.PageContext. pageContext class is an abstract class which stores references to the implicit objects. This class provide methods to set and get attributes.

<% pageContext.setAttribute("i", 10);  %>
<% out.print(pageContext.getAttribute("i"));   %>

exception variable and object

This is a instance of java.lang.Throwable interface. This can be used only if the page is set to a error page using isErrorPage = true. If it is set to true, then you can get the exception messages. 

<%@ page isErrorPage='true' %>
Error Message: <%=exception.toString()%>