JSP response

Basically, JSP response object is an instance of servlet's HttpServletResponse interface. It is used to resolve client request.

Response object can be used to perform various functions such as encode URL, add cookies, add headers, send errors, set content type etc.

Example of JSP Response

In this example, user's password is set in index.jsp file. If the password entered in the form is same then valid.jsp file will execute. If entered password is unmatched then invalid.jsp file will execute.

index.html

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<form action="index.jsp">
Name:<input type="text" name="name"> <br>
Password:<input type="password" name="pass"> <br>
<input type="submit" name="submit">
</form>
</body>
</html>

index.jsp

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<%
String name=request.getParameter("name");
String pass=request.getParameter("pass");
if(pass.equals("abcd"))
{
    response.sendRedirect("valid.jsp");
}
else
{
    response.sendRedirect("invalid.jsp");
}
%>
</body>
</html>

Valid.jsp

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<h1>
<%="Valid user"%>
</h1>
</body>
</html>

invalid.jsp

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<h1>jsp response object method
<%="invalid user" %>
</h1>
</body>
</html>

Here, we already set "abcd" as password. So, if the entered password is correct then only the user is valid.

Output: Here, we already set "abcd" as password. So, if the entered password is correct then only the user is valid.