JSP implicit object response with example - Java @ Desk

Sunday, May 4, 2014

JSP implicit object response with example

JSP implicit object response with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example

Today we will learn, JSP implicit object response.

The response object is an instance of class implementing an javax.servlet.http.HttpServletResponse interface. The response object handles the output that need to be send to the client browser.

Basic example of response is a login page. If the user is validated successfully, the response is the Home page else the response is the Login Page with error message. In case of error, the method sendError is called by passing the HTTP Error Code. For List of Error Codes, click here.

Methods of response object:
1) setContentType : This method sets the content type for the page. Examples are
HTML - "text/html",
XML - "text/xml"
CSV - "text/csv"

2) sendRedirect(String URL) : This method takes the URL parameter in a String format and navigates the client to the URL passed.

3) sendError(int status_code) : This method returns the error back to the client with the Error Code passed. If the response is already committed, then it throws an IllegalStateException exception.

4) getWriter() : This method provides an instance of PrintWriter class. This is use to send the text response back to the client.

5) addCookie(Cookie cookie) : This method is used to add the cookie to the response . For multiple cookies, call this method more than once passing different cookies object

6) addHeader(String name, String value) : This method is used to write the header to the response. If the header is already present, then value is added to the existing header values.

Sample Example :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Object 'response' Example</title>
</head>
<body>

<%
response.sendRedirect("http://javacodeimpl.blogspot.com");
%>
</body>
</html>






No comments:

Post a Comment