Jsp contain the following 9 implicit objects.

implicit object declared type
request javax.Servlet.http.HttpServletRequest(Interface)
response javax.Servlet.http.HttpServletResponse(Interface)
config javax.Servlet.ServletConfig(Interface)
application javax.Servlet.ServletContext(Interface)
session javax.Servlet.http.HttpSession(Interface)
out javax.Servlet.jsp.JspWriter(Abstract class)
page javax.lang.Object(Concrete class)
pageContext javax.Servlet.jsp.pageContext(Abstract class)
exception java.lang.Throwable(Concrete class)

request and response

request and response implicit objects are available as arguments to _jspService() method. All methods of HttpServletRequest and HttpppServletResponse can be applicable on these request and response implicit objects.

Example:
demo.jsp

The Request method is:<%= request.getMethod()%>
The Remote address is:<%= request.getRemoteAddr()%>
The UserName is:<%= request.getParameter("uname")%>
<%
response.setContentType("text/html");
response.setHeader("j2l","java");
%>

application

This implicit object is type of ServletContext,which represents the environment of web application.

Example:

The context parameter username is:
<%=application.getInitParameter("uname");%>

session

This implicit object is of HttpSession type. In every jsp by default, session object is available.

Example:

The session Id:<%=session.getId()%>

We can make session object unavailable to jsp by using page directive.

<%@ page session="false"%>

page

The page implicit object is of java.lang.Object type It is always pointing to current Servlet object

In the generated Servlet this variable has declared as follows

Object page=this;

page implicit object is of object type,hence we are allowed to call only the methods available in Object class and we can’t call Servlet specific methods.Hence page implicit object is the most rarely used implicit object in the jsp.

config

config implicit object is of type servletConfig hence every method of ServletConfig we can apply on the config object.
The following are the methods of ServletConfig

  1. getServletName()
  2. getServletContext()
  3. getInitParameter()
  4. getInitParameterNames()

Example:
demo.jsp

<h1>The logical Name:<%=config.getServletName()%>
Initparameter:<%=config.getInitParameter("uname")%>

web.xml

<web-app>
<servlet>
 <servlet-name>DemoJsp</servlet-name>
 <jsp-file>/demo.jsp</jsp-file>
 <init-param>
  <param-name>uname</param-name>
  <param-value>scott</param-value>
 </init-param>
</servlet>
<servlet-mapping>
<servlet-name>DemoJsp</servlet-name>
<url-pattern>/test</url-pattern>
<servlet-mapping>
</web-app>
  1. http://localhost:9091/WebApp/test
  2. The Logical Name:DemoJsp
    The InitParameter:scott

  3. http://localhost:9091/WebApp/demo.jsp
  4. The Logical Name:jsp
    The InitParameter:null

To reflect Servlet level web.xml configurations in the jsp compulsary we should access by using url-pattern.

pageContext

This implicit Object is of type javax.Servlet.jsp.PageContext.It is an abstract class and vendor is responsible to provide implementation.
PageContext is the child class of jspContext and we can use pageContext object for the following purposes

  1. To get all other jsp implicit objects
  2. For request dispatching purposes
  3. For aattribute management in any scope.

Getting other jsp implicit objects by page context

PageContext class defines the following methods for this

  1. request—->getRequest()
  2. response—>getResponse()
  3. config—–>getServletConfig()
  4. application—>getServletContext()
  5. out—->getOut()
  6. session—->getSession()
  7. exception—->getException()
  8. page—->getPage()

These mthods are not useful with in the jsp but these are useful outside of jsp mostly in customtag handler classes.

Request dispatching by PageContext

PageContext class defines the following methods for this

  1. public void forward(String target)
  2. public void include(String target)


Example:

first.jsp

<h1>This is first jsp</h1
<%pageContext.include("second.jsp")%>

second.jsp

<h1>This is second jsp</h1>

out

This implicit object is of type javax.servlet.jsp.jspWriter. This class is specially designed for jsp’s to write character data to the response.

The main different between jspwrite & printwriter

In the case of printwriter there is no buffering hence if we are adding any character data by PrintWriter it will be added immediately to the response.
But in the case of JspWriter buffering concept is available.Whenever we are writing character data by JspWriter first it will be stored in the buffer without adding to the response directly.Once the response is committed then only entire buffer data will be added to the response.Except this buffer difference there is no other difference between JspWriter and PrintWriter

JspWriter=PrintWriter+Buffer

Example:

<%
Java.io.printwriter pw = response.getwriter();
out.print("One");
PW.print("Two");
out.print("Three");
PW.print("Four");
out.print("Five");
PW.print("Six");
%>

output:

Within the jsp we can use either printwriter or jspwriter but not recommended to use both simultaneously. Jspwriter is the child class of writer hence all methods in writer class all by default available to the jspwriter through inheritance. In addition to these methods jspwriter contains it’s own set of print() & println() method to add any type of data to the response.

Example:

<%
       out.println("the PI value is");
       out.println(3.14);
       out.println("this is exactly");
       out.println(true);
%>