Open In App

JSP | Implicit Objects – request and response

Improve
Improve
Like Article
Like
Save
Share
Report

Perquisite: Introduction To JSP
JSP 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.01
<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 
 

  1. request: This is the object of HttpServletRequest class associated with the request.
  2. response: This is the object of  HttpServletResponse class associated with the response to the client.
  3. config: This is the object of ServletConfig class associated with the page.
  4. application: This is the object of ServletContext class associated with the application context.
  5. session: This is the object of HttpSession class associated with the request.
  6. 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.
  7. page object: The manner we use the keyword this for current object, page object is used to refer to the current translated servlet class.
  8. 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.
  9. 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 Object
The 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"
 
<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"
<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 page
response.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.


Last Updated : 15 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads