"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
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"
JSP response when valid input "Rao" is given
JSP response when invalid input other than "Rao" is given
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
- Using JSP implicit object config Example
- Using implicit object application in JSP with Example
- Using implicit object out in JSP Example
- Using implicit object session JSP Example
- Using implicit object request JSP Example
- Using implicit object response JSP Example
- JSP sendRedirect Example
- Using implicit object exception JSP Example