JSP - SPLessons

Chapter 19

JSP Server Response

JSP Server Response

JSP Server Response – The implicit response object is an instance of HttpServletResponse and for each JSP request, the response is created by the JSP container. The request object is used to send the information and the response object sends the output for processing of request object which is in the form of out stream to the browser. Following are the methods which are used in a servlet.

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.sendRedirect(String address):It sends the redirect response to new JSP depending upon the URL. Following is an example to this method.


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.

JSP Server Response – Following is an example.


<form action="response.jsp">
       Enter Site Name:<input type="text" name="site"/>
                       <input type="submit" value="submit"/>

Here created one text field to enter the name of the site that is Enter Site Name and also created the submit button.

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


  • JSP Server Response – Response is the  HttpServletResponse object.
  • Response object is utilized to update the response between the client and server.
  • isCommited() mehod used to check whether the task performed well or not between the browser and server.