×
☰ See All Chapters

Implicit objects in JSP

We have already learnt from previous chapter that JSP will be converted to servlet. When we look at the generated JSP servlet source code, we can observe that the code contains several object declarations in its _jspService method. These objects are the java objects that the JSP container makes available to the developers in each page and the developer can use them directly without being explicitly declared again. Developers can use these JSP implicit objects as pre-defined variables.

public final class demo_jsp extends org.apache.jasper.runtime.HttpJspBase

                implements org.apache.jasper.runtime.JspSourceDependent {

 

        private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory

                        .getDefaultFactory();

 

        … …

      … …

 

        public void _jspService(

                        final javax.servlet.http.HttpServletRequest request,

                        final javax.servlet.http.HttpServletResponse response)

                        throws java.io.IOException, javax.servlet.ServletException {

 

                final javax.servlet.jsp.PageContext pageContext;

                javax.servlet.http.HttpSession session = null;

                final javax.servlet.ServletContext application;

                final javax.servlet.ServletConfig config;

                javax.servlet.jsp.JspWriter out = null;

                final java.lang.Object page = this;

                javax.servlet.jsp.JspWriter _jspx_out = null;

                javax.servlet.jsp.PageContext _jspx_page_context = null;

 

                try {

                        response.setContentType("text/html");

                        pageContext = _jspxFactory.getPageContext(this, request, response,

                                        null, true, 8192, true);

                        _jspx_page_context = pageContext;

                        application = pageContext.getServletContext();

                        config = pageContext.getServletConfig();

                        session = pageContext.getSession();

                        out = pageContext.getOut();

                        _jspx_out = out;

 

                       

                } catch (java.lang.Throwable t) {

                        … …

                  … …

                        }

                } finally {

                        _jspxFactory.releasePageContext(_jspx_page_context);

                }

        }

}

 

There are 9 implicit objects present in JSP which are listed below:

Object

Type

request

javax.servlet.http.HttpServletRequest

response

javax.servlet.http.HttpServletResponse

out

javax.servlet.jsp.JspWriter

config

javax.servlet.ServletConfig

application

javax.servlet.ServletContext

session

javax.servlet.http.HttpSession

pageContext

javax.servlet.jsp.PageContext

page

javax.servlet.jsp.HttpJspPage

exception

java.lang.Throwable

request implicit object

<html>

<head>

<title>request Implicit Objects</title>

</head>

<body>

        <%

                String name = request.getParameter("name");

                out.println("Name: " + name);

        %>

</body>

</html>

response implicit object

In a servlet, before sending output, it is required to call the setContentType of the HttpServletResponse as below:

      response.setContentType("text/html");

 

In JSP, this is done automatically in the generated JSP servlet class, as follows:

      response.setContentType("text/html;charset=ISO-8859-1");

 

Below example uses the sendRedirect method of the HttpServletResponse to redirect the user.

 

        <%

                response.sendRedirect("https://www.newriders.com");

        %>

out implicit object

In a servlet it is needed to call the getWriter method of the HttpServletResponse interface to obtain a PrintWriter to write output as follows:

PrintWriter out = response.getWriter();

But in JSP, we have an out object of a JspWriter object implicitely. We call either its print method or its println method to send text or other data to the client browser.

<html>

<head>

<title>request Implicit Objects</title>

</head>

<body>

        <%

                out.print("Hello World");

        %>

</body>

</html>

config implicit object

The config implicit object represents object of ServletConfig. To get this in a servlet we have to call  getServletConfig method.

application implicit object

The application implicit object represents object of ServletContext object. To get this in a servlet we have to call getServletContext method.

pageContext implicit object

The pageContext implicit object represents object of javax.servlet.jsp.PageContext object.

The PageContext class has methods that are used to create other objects. Below are the important methods of PageContext.

 

public abstract java.lang.Object getPage()

Returns current value of the page object (In a Servlet environment, this is an instance of javax.servlet.Servlet).

public abstract ServletRequest getRequest()

Returns current value of the request object (a ServletRequest).

public abstract ServletResponse getResponse()

Returns current value of the response object (a ServletResponse).

public abstract ServletConfig getServletConfig()

Returns ServletConfig instance.

public abstract ServletContext getServletContext()

Returns ServletContext instance.

public abstract HttpSession getSession()

Returns current value of the session object (an HttpSession).

public abstract JspWriter getOut()

Returns current value of the out object (a JspWriter).

page implicit object

The page implicit object represents object of HttpJspPage interface object.  The JspPage is the interface that must be implemented by all JSP servlet classes. HttpJspPage interface directly extends the JspPage interface.  The JSPPage interface has two methods, JspInit and JspDestroy. HttpJspPage has only one method _ jspService.

exception implicit object

The exception object is available only on pages that have been defined as error pages. To configure page as error page we have to use the below code in JSP page.

<%@ page isErrorPage="true"%>

Example

<%@ page isErrorPage="true"%>

<html>

<head>

<title>Exception Page</title>

</head>

<body>

        <%= Error Page %>

</body>

</html>

 

 

 


All Chapters
Author