implicit object response JSP Example


"response" is one of the 9 implicit objects, JSP supports. It is an object of javax.servlet.http.HttpServletResponse interface. This object is used by the Programmer to send cookies to the client, to use HTTP status codes in programming and to redirect the client to a new page.

Following are some important methods of HttpServletResponse that can be used by JSP response object straight away.

  • public abstract void sendRedirect(String) throws java.io.IOException;
  • public abstract void setStatus(int, String);
  • public abstract void setHeader(String, String);
  • public abstract void addCookie(Cookie);
  • public abstract void sendRedirectString) throws java.io.IOException;

Following example uses sendRedirect(String) method to send a file to client.

The JSP file reads user name from the client HTML, validates and as per the validation sends an appropriate HTML file to client.

1. Client Program File Name: ResponseResult.html

Enter Your Name

2. Server-side JSP file File Name: UsingResponse.jsp

<%
  String str = request.getParameter("t1");
  
  if(str.equals("Rao"))
  {
    response.sendRedirect("Valid.html");
  }
  else
  {
    response.sendRedirect("Invalid.html");
  }
%>

3. File Name: Valid.html

  

Your name is Valid

4. File Name: Invalid.html

   

Your name is Invalid

HTML file when entered valid input "Rao"

ima

JSP response when valid input "Rao" is given

ima1

JSP response when invalid input other than "Rao" is given

ima2

String str = request.getParameter("t1");

getParameter("t1") of implicit object request returns the value entered by the user in the text box t1.

response.sendRedirect("Valid.html");

sendRedirect("Valid.html") method returns the Valid.html file to the client.

    9 Implicit Objects with Examples

  1. Using JSP implicit object config Example
  2. Using implicit object application in JSP with Example
  3. Using implicit object out in JSP Example
  4. Using implicit object session JSP Example
  5. Using implicit object request JSP Example
  6. Using implicit object response JSP Example
  7. JSP sendRedirect Example
  8. Using implicit object exception JSP Example

JSP View All

Leave a Comment

Your email address will not be published.