Implicit Variables

Implicit Variables:

In JSP we can use some variables without we even declaring, such variables are called as Implicit Variables. 
or 
The variables which can be used in JSP's without declaration, we call such variables as Implicit Variables.

There are 9 Implicit Variables available in JSP. They are:
  1. request
  2. response
  3. pageContext
  4. session
  5. application
  6. config
  7. out
  8. page
  9. exception
Let us see each one in detail.

request:

This implicit variables can be used directly in the JSP scriptlet without even declaring. This variable represents the HttpServletRequest of servlets. The following is the example for request variable.
request implicit variable
fig: request implicit variable example

response: 

response implicit variable represents the HttpServletResponse object of servlets. The following is the example of response implicit variable.
response implicit variables
fig: response implicit variables example

out: 

This out implicit variable is used to display the output to the user. out variable is of type JspWriter. JspWriter is a class which is available in javax.servlet.jsp package. JspWriter is just similar to the PrintWriter in Servlets
There are two methods which can be used with out variable. They are: 1) println 2)write

The following example shows the usage of out variable.
out Implicit variable
fig: out Implicit variable example

config: 

This config variable belongs to ServletConfig datatype. By using ServletConfig we can remove hardcoding. ServletConfig is an object used by servlet container to pass the information during initialization. The following figure shows you the usage example of config variable.

application:

This application implicit variable can be used directly in the jsp. application variable represents ServletContext. ServletContext object contains set of methods which are used to interact with ServletContainer. By using these methods we can find the information about ServletContainer. 

session:

This implicit object represents HttpSession. This variable is of type HttpSession.

page:

page implicit variable holds the currently executing Servlet object for the corresponding JSP. page variable is of type Object.

exception:

This implicit variable can be used only in error pages. When we try to use this implicit variable in JSP which is not an error page. We will get an error message. This variable of type Throwable.

pageContext: 

By using this pageContext implicit variable we can get any other implicit variable/object.                                                   

1 comment: