response implicit Object in jsp

Introduction –: we know that the implicit object are automatic created. In the last post we learn about request implicit object in jsp. Now we learn about response implicit object.

response object is an instance of javax.servlet.http.HttpServletResponse object. On every request, the response object is created by the JSP Engine.The response object is used to add or set a response or to send a response to another page.

response implicit Object in jsp

Methods of response Implicit Object in JSP

There are some method of response implicit object in jsp. These are the

  • setContentType(String type)
  • sendRedirect(String URL)
  • addHeader(String name, String value)
  • setHeader(String name, String value)
  • containsHeader(String name)
  • addCookie(Cookie value)
  • sendError(int status_code) and sendError(int status_code, String message)
  • addIntHeader(String name, int value)
  • setIntHeader(String name, int value)

SetContentType-: This method encoding MIME (Multipurpose Internet Mail Extensions) type and character for JSP Page.This is set When the time comes to send the content to the client browser, the client browser is informed about the type of content to be sent from the web container. That content; setContentType () method is given here below..

<%
 response.setContentType("text/plain");
 response.setContentType("text/html");
 response.setContentType("image/png");
 %>

sendRedirect()-: This method sends redirect response to client. If any URL is given on the parameter then the current JSP is redirected to the given URL from Page

<% response.sendRedirect("http://netnic.org"); %>

addHeader()-:This method is used to add a header on response. Here are the header name and its value pair are given.

<% response.addHeader("Web", "netnic.org"); %>

setHeader()-: This method sets the header name and its value. This method is used to override the value of the name added to the response. For example

<% response.addHeader("Web", "netnic.org"); %> 
<% response.setHeader("Web", "lyricspig.com"); %>

containsHeader()-:This method returns the header to Boolean value by whether the header is added or set.. For example

<% response.setHeader("Web", "netnic.org"); %> 
<% out.print(response.containsHeader("Web")); %>

addCookie()-: The addCookie () method is used to add a cookie on response. for example

<% response.addCookie(Cookie netnic); %>

sendError() and sendError()-:The sendError () method is used with status_code and error message. for example..

Sample.jsp
 <% response.sendRedirect("redirect.jsp"); %>
redirect.jsp
 <% response.sendError(406, "Not Acceptable"); %>

addIntHeader ()-: This method is used to add integer value with name on the response header. For example of this method ..

<% 
response.setIntHeader("Refresh", 1);
 out.print((Math.random())); 
%>

setIntHeader()-: This method is used to set the integer value with the name on the response header. The value added here can be override by name. for example

<%
     response.addIntHeader("Refresh", 10);
      response.setIntHeader("Refresh", 1); 
      out.print((Math.random())); 
%>

Leave a Comment