9 JSP Implicit Objects and When to Use Them

If you are new to JSP then your first question would be what are implicit objects in JSP? Well, implicit objects in JSP are Java object which is created by Servlet containers like the tomcat or Jetty during translation phase of JSP, when JSP is converted to Servlet. These objects are created as local variables inside the service() method generated by the container. The most important thing about the implicit objects is to remember that these objects are available to any JSP developer inside the JSP page and they can do a lot of things with implicit objects and expression language which was only possible with scriptlet earlier. That's why they are known as implicit objects. 

Why you should know about implicit objects? you should know about them because the goal is to minimize Java code inside JSP, more you know about implicit objects, expression language, and tag libraries like JSTL, more cleaner your JSP would be. How many implicit objects are available on the JSP page? 

There are a total of 9 implicit objects available in any JSP and they are known as for request, response, out, session, application, config, pageContext, page, and exception. Each of these is an object of various classes from the Servlet package like request is an object of HttpServletRequest and you can do all request-related stuff using this object. 

Similarly, the response is an object of HttpServletResponse class and can be used to access an attribute from the response or to add an attribute, variables into the response object. In the next section, we will learn the meaning of these 9 JSP implicit objects and how to use them properly. Btw, if you are a beginner to servlet and JSP then you can also start with these free JSP and Servlet courses from Coursera and Udemy. 





Meaning of 9 JSP Implicit Objects

Here are the 9 implicit objects which are available inside every JSP page and you can use them whenever you need them. For example, when you need to extract a request parameter or store something on response. 

9 JSP Implicit Objects and When to Use Them


1. request

a request is an object of the HttpServlet request. Each time your JSP is accessed by the client, Servlet Container creates an object of this class. You can use request objects to access HTTP header information including form data, cookies, and various query parameters as part of HTTP requests. You can also know whether HTTP methods of requests like GET or POST.

2. response

the response is an object of the HttpServlet response. Just like a request, whenever you access the JSP page, the Servlet container also creates a new object of HttpServletResponse and assigns it to the response implicit object. You can use the response to add HTTP header, timestamp into the HTTP response. 


3. out

out is an object of PrintWriter class and you can use this object to write anything on console or response data. 


4. session

session reprsent HttpSession object. You can use store user data inside sessions like username etc.


5. application

application is an object of ServletContext class. This is an important class and again can be used to store global variables which you want to access from any Servlet. 


6. config

config is an object of ServletConfig class and you can use this to pass initialization parameters to Servlet. 


7. pageContext

pageContext is an object of JspWriter class


8. page

page is equivalent to this, the current object of Servlet class


9. exception

This is an object of the Exception class and allows JSP to access exception-related data.


JSP Implicit Object Example

I leave this to you, try to use this on your JSP page if you are stuck then post your code in the comments, I will help you to fix the problem. 


That's all about what is implicit objects in JSP, Why you should use implicit objects, and how to use them to minimize Java code inside JSP. If you have followed our JSP implicit object example, then you know how easy it is to retrieve information from these implicit objects using JSP expression language.

Related Servlet and JSP interview questions for Java programmers

  • 20+ Spring and REST Interview Questions with Answers (questions
  • Difference between ServletContext and ServletConfig in Servlet? (answer
  • Can you declare Constructor inside Servlet class? (answer
  • Difference between jsp:include and jsp:forward action in JSP? (answer
  • 5 Courses to learn Servlet and JSP for free (courses
  • Difference between GenericServlet and HttpServlet in Servlet API? (answer
  • Difference between the web server, application server, and Servlet container? (answer
  • How to manage client sessions in a Servlet application? (answer
  • Difference between include directive and include action in JSP? (answer
  • 20+ Spring MVC Interview Questions for Java Developers (questions


Thanks for reading this article so far. If you like this JSP tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.