JSP | Implicit Objects - request and response - GeeksforGeeks

15 Feb, 2021
Perquisite: Introduction To JSPJSP stands for Java Server Pages, and it’s a server side technology. It’s used for creating web applications and dynamic web content. The main property of JSP is that we can insert our java code inside our HTML page using JSP tag. JSP provides you platform-independent pages. 
html
<%@ page language = "java" contentType = "text/html; charset = UTF-8"    pageEncoding = "UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"><title>Insert title here</title></head><body> <!-- Here we are going to insert our Java code--> <% Geeks saying hello to JSP %></body></html>
Implicit objects are a set of Java objects that the JSP Container makes available to developers on each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.JSP provide you Total 9 implicit objects which are as follows  
request: This is the object of HttpServletRequest class associated with the request.response: This is the object of  HttpServletResponse class associated with the response to the client.config: This is the object of ServletConfig class associated with the page.application: This is the object of ServletContext class associated with the application context.session: This is the object of HttpSession class associated with the request.page context: This is the object of PageContext class that encapsulates the use of server-specific features. This object can be used to find, get or remove an attribute.page object: The manner we use the keyword this for current object, page object is used to refer to the current translated servlet class.exception: The exception object represents all errors and exceptions which is accessed by the respective jsp. The exception implicit object is of type java.lang.Throwable.out: This is the PrintWriter object where methods like print and println help for displaying the content to the client.
request: This is the object of HttpServletRequest class associated with the request.
response: This is the object of  HttpServletResponse class associated with the response to the client.
config: This is the object of ServletConfig class associated with the page.
application: This is the object of ServletContext class associated with the application context.
session: This is the object of HttpSession class associated with the request.
page context: This is the object of PageContext class that encapsulates the use of server-specific features. This object can be used to find, get or remove an attribute.
page object: The manner we use the keyword this for current object, page object is used to refer to the current translated servlet class.
exception: The exception object represents all errors and exceptions which is accessed by the respective jsp. The exception implicit object is of type java.lang.Throwable.
out: This is the PrintWriter object where methods like print and println help for displaying the content to the client.
In this article, two of the main objects which are request and response are discussed 
request ObjectThe JSP request is an implicit object which is provided by HttpServletRequest. In the servlet, we have to first import javax.servlet.http.HttpServletRequest then we have to create its object for taking input from any HTML form as. Syntax :
import javax.servlet.http.HttpServletRequest;

public class LoginServlet extends HttpServlet 
{  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException 
          { 
              HttpSession session = request.getSession()
          }
}
In JSP, the request object is implicitly defined so that you don’t have to create an object. JSP request object is created by the web container for each request of the client. It’s used for getting the parameter value, server name, server port, etc.In the below example we are using a request object to display the username. 
html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action = "Geeks.jsp"> <input type = "text" name = "username"> <input type = "submit" value = "submit"><br/> </form></body></html>
Output  
 
response object
This is the HttpServletResponse object associated with the response to the client. The response object also defines the interfaces that deal with creating new HTTP headers. Through this object the JSP programmer can add new cookies or date stamps, HTTP status codes, etc.  
html
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%  String name=request.getParameter("username"); out.print("Welcome "+ name); %>   </body></html>
Output  
In JSP the response object is implicitly defined, so you don’t have to create an object. JSP response object is created by the web container for each request of the client. It basically is used for redirecting to any other resource. In the below example we use the response object to send the user on Geeksforgeeks homepage. 
html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>GeeksforGeeks</title></head><body><%  //below line in JSP redirect you to geeksforgeeks pageresponse.sendRedirect("geeksforgeeks.org");%></body></html>
Output  
Advantage of JSP over servlet :  
Servlets are difficult to code than JSP. In another way, we can say, JSP is the replacement for Servlets.
In Servlets, both static code and dynamic code are put together. In JSP, they are separated.
The objects of PrintWriter, ServletConfig, ServletContext, HttpSession and RequestDispatcher etc. are created by the Programmer in Servlets. But in JSP, they are built-in and are known as implicit objects.
Disadvantage :  
JSP pages require more memory to hold the page.
The output is in HTML which is not rich for viewers.
easeit
Java-JSP
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
Stream In Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Singleton Class in Java
Multithreading in Java
Set in Java