CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

JSP – Implicit Objects

We know that every JSP page turned into an equivalent Servlet. Every JSP equivalent Servlet must be extended from HttpJspBase class. HttpJspBase class is an abstract class with all implicit objects are defined. Those implicit objects are called JSP implicit objects. Because objects’ names are already defined, we have to use them as it is. In JSP there are nine implicit objects are available for every JSP page.

In this article, I am going to discuss each individual object. The implicit objects are:

  • request
  • response
  • config
  • application
  • out
  • page
  • exception
  • session
  • pageContext

request Implicit Object

When the user requests a JSP page, the web browser or client will send an HTTP Request to the server.

JSP’s request object enables to access the HTTP Request within the JSP page to get the parameters information, client information, etc. the request object is an instance of a class that implements the interfaces HttpServletRequest and ServletRequest. JSP Engine will pass this request object to the life-cycle method _jspService

_jspService(HttpServletRequest request, HttpServletResponse response)

to provide the requested service to the user or client. By using the request object we can call all HttpServletRequest methods and we can also place additional attributes in this object.

Let’s take a simple example: When the user requests for a JSP page with additional parameters like this “http://localhost:8080/sample.jsp?key=xs23eS”; the web-server receives this request and attempts to process the “sample.jsp” page. Within the JSP page, we can write the below statement to access the parameter “key” value.

<%=request.getParameter("key")%>

The above line returns the value of the given parameter “key” as “xs23eS” which we passed through the URL.

By using the request object we can get the RequestDispatcher object which is most widely used in servlets. Below code will get this object from the request object:

<%
   RequestDispatcher rd = request.getRequestDispatcher("sample.jsp");
%>

RequestDispatcher object is used to send the request to another servlet or JSP.

We will discuss more this in our upcoming articles.

response Implicit Object

For each request that comes from the web browser or client, JSP Engine (actually Servlet Engine provides a response object) generates a response object. With this response object, we can provide the processed information back to the requester (web browser or client).

The response object is an instance of a class that implements the HttpServletResponse and ServletResponse interfaces. By using this response object we can call all HttpServletResponse methods.

For example Below statement will set the content type of the response. Based on the response’s content type the web browser or client interprets the response comes from the server.

<% response.setContentType("text/html") %>

config Implicit Object

The config object is an instance of a class that implements ServletConfig interface. Generally, we use config object to get initialization parameters configured in the “web.xml” file (In Tomcat web-server). We can get these parameters by calling getInitParameter() or getInitParameterNames() methods through config object.

To get the name of the servlet, we can use the below statement:

<%=config.getServletName()%>

The above statement returns the name of the servlet.

We can also get the ServletContext object by using this config object. ServeltContext object contains the information about the environment in which the servlet is running.

application Implicit Object

The application object is used to access application-level resources shared within the web application. The application object is an instance of a type class that implements the ServletContext interface. The application object is used to get the context parameters and context attributes. For example below statement

<%=application.getServerInfo()%>

returns the name and version of the servlet container in which the servlet is running.

Through application objects also we can get the RequestDispatcher object.

<%
RequestDispatcher rd =
application.getRequestDispatcher("sample.jsp");
%>

See in the request object section to know more about RequestDispacther object.

out Implicit Object

There must be a simple way to send back the information to the web browser or client. Tomcat provides out the implicit object for this purpose. out object is an instance of the class JspWriter.

For example: By using out object we can write our response onto the web browser by calling the below method:

<%out.println("hello");%>

The above code simply prints a “hello” message in the web browser.

page Implicit Object

Thepage object is of type java.lang.Object. The page object is nothing but “this” in JSP equivalent servlet. By using page object we can call any method of the servlet. But before calling any method we need to type-cast it to Servlet because the page object is of type Object.

By using page object we can call any methods of Servlet and HttpServlet because the the JSP equalent servlet implements HttpServlet. Below example:

<%=((HttpServlet)page).getServletName()%>

will return the name of JSP equalent servlet. Remember that servlet name and servlet class name are different.

exception Implicit Object

The exception object is of type java.lang.Throwable class. By using exception object we can know stack trace or root cause or other details of raised exception. We can’t use this object on all JSP pages. The JSP pages which are for error pages (isErrorPage attribute of page directive set to true.) are only available to use this exception object.

For example: To get the error message we can use the below statement:

<%=exception.getMessage()%>

The above code returns the error message string.

session Implicit Object

The session object is an instance of a class that implements an interface HttpSession. By default, every JSP page can use a session object; which means the session is automatically enabled. We can disable the session by using the session attributes of the page directive.

<%=session.isNew()%>

The above code returns “true” if the session is newly created otherwise returns “false”.

pageContext Implicit Object

pageContext object is of type PageContext class. All implicit objects are created from the pageContext object. So, we can say that the pageContext object maintains all references of implicit objects. Whatever we can do with all implicit objects the same things we can do with pageContext objects. By using this object we can set or remove attributes in different scopes.

<%pageContext.getAttribute("name",PageContext.SESSION_SCOPE);%>

The above code searches for the given attribute in session scope; if it finds it, it will return the attribute, otherwise returns null.

Let’s discuss more JSP concepts in our upcoming articles.

JSP – Implicit Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top