JSP Implicit Objects - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

JSP Implicit Objects

Share This

Now, these objects are given to you for free, it's not like you have to go ahead and pay to use them (like you do for some APIs), so there is no need to create them, you can just use them in your JSP pages. Here are a couple of the most commonly used server objects:



  • request: Contains information about the HTTP request headers and the form data. The request object is an instance of a javax.servlet.http.HttpServletRequest object. The request object represents a client's request and is a reference to the HttpServletRequest object passed into a HttpServlet's appropriate service method.

  • response: This object is used for sending the HTTP-specific response for the received request. The response object is an instance of a javax.servlet.http.HttpServletRequest object. The response object represents a response to a client's response and is a reference to the HttpServletResponse object passed into a HttpServlet's appropriate service method.

  • out: This object can be used for adding data to the HTML page using out.println.

  • session: This object is used to maintain sessions for all the users of your web application. The session object is an instance of a javax.servlet.http.HttpSession object. By default, JSP creates a keep session context with all clients. The session object is a convenience object for use in scripting elements and is the equivalent of calling the HttpServletRequest getSession() object.

  • application: Shared data for all the users of your web application. The application implicit object is an instance of a javax.servlet.ServletContext object. The application object represents a Servlet's view of a Web Application and is equivalent to calling the ServletConfig getServletContext() method.

  • config: The config object is an instance of a javax.servlet.ServletConfig object. Same as with Servlets, JSP can take advantage of initial parameters provided in a Web Application Deployment Descriptor.

  • pageContext: A PageContext object represents the context of a single JavaServer Page including all the other implicit objects, methods for forwarding to and including Web Application resources, and a scope for binding objects to the page.

  • page: The page implicit object represents the current class implementation of the page being evaluated. If the scripting language of the page is Java, which by default it is, the page object is equivalent to the this keyword of a Java class.


All of these implicit Objects maps to something from the Servlet/JSP API. You'll just have to research the object you require and then use it on your JSP Page.


Now you have a regular browser communicating with your JSP page. The browser will send an HTTP request object to the JSP. This object will contain the header information and the body information. The JSP will process this information and send back the response, creating a proper request-response communication channel.


Now let's take a look at how to use the request object. We'll use the existing project.


In the Web-Content folder, create a new file and name it whatever you want. Since we are trying to test the built-in objects I'll be naming the file builtin-test.jsp. Name your file and click finish. Now populate the file with the following code snippet:

Source Code of builtin-test.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String var = request.getParameter("x"); out.print(var); %> <% String contentType = response.getContentType(); out.print(contentType); %> <% long time = session.getCreationTime(); out.print(time); %> <% String contextPath = application.getContextPath(); out.print(contextPath); %> <% contextPath = config.getServletContext().getContextPath(); out.print(contextPath); %> <% String pageCon = pageContext.getELContext().getClass().getName(); out.print(pageCon); %> <% String pageName = page.getClass().getName(); out.print(pageName); %> </body> </html>

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.