JSP | Implicit Objects – request and response
Perquisite: Introduction To JSP
JSP stands for Java Server Pages and its a server side technology. Its 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 provide you platform independent pages.
<%@ 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> <!-- Here we are going to insert our Java code--> <% Geeks saying hello to JSP %> </body> </html> |
JSP provide you Total 9 implicite object which are as follows
- request
- response
- config
- application
- session
- page context
- page object
- exception
- out
In this article, two of the main objects which are request and response are discussed
request Object
The JSP request is implicit object which is provided by HttpServletRequest. In 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 any object. JSP request object is created by the web container for each request of client. Its used for getting the parameter value, server name, server port etc.
In the below example we are using request object to display the username.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <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.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <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 any object. JSP response object is created by the web container for each request of client. Its basically is used for redirecting to any another resource.
In the below example we use response object to send the user on Geeksforgeeks homepage.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>GeeksforGeeks</title> </head> <body> <% //below line in JSP redirect you to geeksforgeeks page response.sendRedirect("geeksforgeeks.org"); %> </body> </html> |
Output

Advantage of JSP over servlet :
- Servlets are difficult to code than JSP. Other way, we can say, JSP is replacement of 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.
- Output is in HTML which is not rich for viewers.
Recommended Posts:
- Android | Implicit and Explicit Intents with Examples
- Android | How to Request permissions in Android Application?
- Classes and Objects in Java
- How are Java objects stored in memory?
- Different ways to create objects in Java
- Understanding Classes and Objects in Java
- How to swap or exchange objects in Java?
- Passing and Returning Objects in Java
- equals() on String and StringBuffer objects in Java
- Comparison of Autoboxed Integer objects in Java
- Using TreeMap to sort User-defined Objects in Java
- Count number of a class objects created in Java
- How to prevent objects of a class from Garbage Collection in Java
- Referencing Subclass objects with Subclass vs Superclass reference
- Java.util.Objects class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



