response.setContentType():It tells the MIME (Multipurpose Internet Mail Extensions) type which is used for body in response text/html. Following is an example to this method
response.setContentType("text/html");
response.sendRedirect(String address):It sends the redirect response to new JSPÂ depending upon the URL. Following is an example to this method.
response.sendRedirect("https://www.splessons.com");
response.addCookie(Cookie cookie):This method is used to add cookies to the response. Following is an example to this method.
response.addCookie(Cookie UserName);
sendError(int error_code, String message):It is used to send the error message depends on the error_code. Following is an example to this method.
response.sendError(500, "Internal server error");
isCommitted():It checks the response whether it is sent to the client or not.
 index.html
<html> <body> <form action="response.jsp"> Enter Site Name:<input type="text" name="site"/> <input type="submit" value="submit"/> </form> </body> </html>
Here created one text field to enter the name of the site that is Enter Site Name and also created the submit button.
response.jsp
<% String site = request.getParameter("site"); response.sendRedirect("http://www."+site+".com"); %>
Here used getparameter(“site”) method to retrieve the site name and used response.sendRedirect() method to get the web site url.
Output
Output will be as follows, where enter the valid site name and click on submit button.
After click on submit button, the entered web site will be opened.
JSP Server Response – Following is the difference between forward() and sendRedirect() methods.
forward() | sendRedirect() |
---|---|
It works at server side. | It works at client side. |
It sends the same request and response objects to another servlet. | It sends a new request. |
Works within the server only. | It can be used within and outside the server. |