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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<%@ 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>

chevron_right


JSP provide you Total 9 implicite object which are as follows



  1. request
  2. response
  3. config
  4. application
  5. session
  6. page context
  7. page object
  8. exception
  9. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<%@ 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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.