Home Tutorial Java Jsp JSP implicit object "response"

 
 

Share on Google+Share on Google+
JSP implicit object "response"
Posted on: June 23, 2010 at 12:00 AM
Advertisement
In this Section, we will discuss about implicit object out & it's methods.

JSP implicit object "response"

In this section, we will discuss about JSP implicit object "response" with an example. Using 'response' object , reply is sent back to the browser. It is an implicit object of class "HttpServletResponse" class and using this object, response parameters can be modified or set. The response object handles the output of the client. The response object is generally used by cookies. The response object is also used with HTTP Headers.

Methods of response object

  • setContentType()
  • addCookie(Cookie cookie)
  • addHeader(String name, String value)
  • containsHeader(String name)
  • setHeader(String name, String value)
  • sendRedirect(String)
  • sendError(int status_code)

setContentType():

The "setContentType()" method of response object is used to set the MIME type and character encoding for the page.

Example :    response.setContentType("text/html");

addCookie(Cookie cookie):

This method is used to add the specified cookie to the response . If you wants to add more than one cookie, then use this method by calling it as many times as required.

Example :   response.addCookie(Cookie roseindia) ;

addHeader(String name, String value): 

This method is used to write the header to the response as pair of name and value. If the header is already present, then value is added to the existing header values. General syntax of addHeader() of response object is as follows:

                   response.addHeader(String name, String value)

Example :  response.addHeader("Author", "Roseindia") ;

containsHeader(String name):

The "containsHeader()" method of response object is used to check whether the header, given as parameter, already included to the response or not. If the named response header is set then it returns a true value. If the named response header is not set, the value is returned as false.

Example : response.containsHeader(String Author)

setHeader(String name, String value):

This method is used to write the header to the response as pair of name and value a string. If the header is already present, then the original value is replaced by the current value given as parameter in this method. General syntax of "setHeader" of response object is as follows

                  response.setHeader(String name, String value)

Example:  response.setHeader("Content_Type","text/html");

sendRedirect(String):

This method is used to send a response which redirect client to new page. But one must note that if the JSP, in execution, has already sent page content to the client, then the "sendRedirect()" method of response object will not work and will fail. General syntax of sendRedirect of response object is as follows:

                   response.sendRedirect(String)

Example :  response.sendRedirect("http://www.roseindia.net/error.html") ;

sendError(int status_code , java.lang.String msg) : 

Sends an error response to the client using the specified status code and descriptive message. If the response has already been committed, this method throws an "IllegalStateException". After using this method, the response should be considered to be committed and should not be written to.

EXAMPLE USING sendRedirect() METHOD :

In this example, first page is login html page which sends login info. to the JSP page which redirects to another page according to the correct or incorrect login. It has two separate pages - one for incorrect login & another for correct login.

response-redirect.html

<html>

<head>

<title>Redirecting Page</title>

</head>

<body>

<form method = "post" action = "response-redirect-main.jsp">

<font size = 4>Enter your name<input type = "text"

name = "name"></font><br>

<font size = 4>Enter your password<input type="password"

name = "pwd" ></font><br><br>

<input type = "submit"

name = "submit" value = "submit" >

</form>

</body>

</html>

response-redirect-main.jsp

<%

String name = request.getParameter("name");

String password = request.getParameter("pwd");

if(name.equals("Williams") && password.equals("abcde"))

{

response.sendRedirect("response-redirect-success.html");

}

else

{

response.sendRedirect("response-redirect-fail.html");

}

%>

response-redirect-success.html

<html>

<head>

<title>Successful Login</title>

</head>

<body>

<font size = 6 color=red>Hello Williams</font>

</body>

</html>

response-redirect-fail.jsp

<html>

<head>

<title>Failed Login</title>

</head>

<body>

<font size = 4 color=red>LOGIN UNSUCCESSFULL.You are not Williams.</font>

</body>

</html>

OUTPUT :

IF LOGIN IS CORRECT-->

IF LOGIN IS INCORRECT-->

DOWNLOAD SOURCE CODE

Advertisement

Related Tags for JSP implicit object "response":


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com