JSP Implicit Objects: Complete Tutorial

JSP Implicit Objects: Complete Tutorial

What is JSP Implicit object?

  • JSP implicit objects are created during the translation phase of JSP to the servlet.
  • These objects can be directly used in scriplets that goes in the service method.
  • They are created by the container automatically, and they can be accessed using objects.

How many Implicit Objects are available in JSP?

There are 9 types of implicit objects available in the container:

  1. out
  2. request
  3. response
  4. config
  5. application
  6. session
  7. pageContext
  8. page
  9. exception

Lets study One By One

out

  • Out is one of the implicit objects to write the data to the buffer and send output to the client in response
  • Out object allows us to access the servlet’s output stream
  • Out is object of javax.servlet.jsp.jspWriter class
  • While working with servlet, we need printwriter object

Example:

Output:

  • In the output, we get the values of num1 and num2

Request

  • The request object is an instance of java.servlet.http.HttpServletRequest and it is one of the argument of service method
  • It will be created by container for every request.
  • It will be used to request the information like parameter, header information , server name, etc.
  • It uses getParameter() to access the request parameter.

Example:

Implicit_jsp2.jsp(form from which request is sent to guru.jsp)

Guru.jsp

Code Line10-11: It is action jsp where the request is processed, and username is taken from form jsp.

When you execute the above code, you get the following output

Output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you write test and click on the submit button, then you get the following output “Welcome Test.”

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Response

  • “Response” is an instance of class which implements HttpServletResponse interface
  • Container generates this object and passes to _jspservice() method as parameter
  • “Response object” will be created by the container for each request.
  • It represents the response that can be given to the client
  • The response implicit object is used to content type, add cookie and redirect to response page

Example:

Explanation of the code:

In web.xml

Code Line 14-17: In web.xml we have mapping of servlets to the classes.

Implicit_jsp5.jsp

Code Line 10-11: To get the name of the servlet in JSP, we can use config.getServletName, which will help us to get the name of the servlet.

When you execute the above code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • Servlet name is “GuruServlet” as the name is present in web.xml

Application

  • Application object (code line 10) is an instance of javax.servlet.ServletContext and it is used to get the context information and attributes in JSP.
  • Application object is created by container one per application, when the application gets deployed.
  • Servletcontext object contains a set of methods which are used to interact with the servlet container.We can find information about the servlet container

Example:

Code Line 11: we are setting the attribute user in the session variable, and that value can be fetched from the session in whichever jsp is called from that (_jsp8.jsp).

Code Line 12: We are calling another jsp on href in which we will get the value for attribute user which is set.

Implicit_jsp8.jsp

Code Line 11: We are getting the value of user attribute from session object and displaying that value

When you execute the above code, you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you click on the link for the username. You will get the following output.

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • When we click on link given in implicit_jsp7.jsp then we are redirected to second jsp page, i.e (_jsp8.jsp) page and we get the value from session object of the user attribute (_jsp7.jsp).

pageContext:

  • This object is of the type of pagecontext.
  • It is used to get, set and remove the attributes from a particular scope

Scopes are of 4 types:

  • Page
  • Request
  • Session
  • Application

Example:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • The output will print “student name is gurustudent”.

Page

  • Page implicit variable holds the currently executed servlet object for the corresponding jsp.
  • Acts as this object for current jsp page.

Example:

In this example, we are using page object to get the page name using toString method

Output:

  • Output is string name of above jsp page

Exception

  • Exception is the implicit object of the throwable class.
  • It is used for exception handling in JSP.
  • The exception object can be only used in error pages.

    Example:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

We are getting ArrayIndexOfBoundsException in the array where we are getting a num1 array of the fifth element.

top
X
Welcome to Our Website
Welcome to WPBot
wpChatIcon