Pr.Pg Next Pg

Methods to Set HTTP Response Header tutorials

  • There are following methods which can be used to set HTTP response header in your servlet program. These method are available with HttpServletResponse object.

 

S.N.

Method

Description

1

String encodeRedirectURL(String url)

 

Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.

2

String encodeURL(String url)

 

Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

3

boolean containsHeader(String name)

 

Returns a boolean indicating whether the named response header has already been set.

4

boolean isCommitted()

 

Returns a boolean indicating if the response has been committed.

5

void addCookie(Cookie cookie)

Adds the specified cookie to the response.

6

void addDateHeader(String name, long

date)

 

Adds a response header with the given name and date-value.

7

void addHeader(String name, String

value)

Adds a response header with the given name and value.

8

void add IntHeader(String name, int

value)

 

Adds a response header with the given name and integer value.

9

void flushBuffer()

Forces any content in the buffer to be written to the client.

10

void reset()

 

Clears any data that exists in the buffer as well as the status code and headers.

11

void resetBuffer()

 

Clears the content of the underlying buffer in the response without clearing headers or status code.

12

void sendError(int sc)

 

Sends an error response to the client using the specified status code and clearing the buffer.

13

void sendError(int sc, String msg)

 

Sends an error response to the client using the specified status.


 

 

Example: Servlet program to auto refresh the page using setHeader method

Sam Sir

//Program name ResponseHeader.java

// Program to auto refresh the page using setHeader method

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class ResponseHeader extends HttpServlet

{

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException

{

Date date = new Date();

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html><body>");

out.println("<b>Current Date: </b>"+ date);

out.println("</body></html>");

response.setHeader("Refresh", "10");

}

}

Output

setHeader

 


 

Example: Servlet program a servlet that uses addDateHeader method

Sam Sir

//Program name test007.java

// Program a servlet that uses addDateHeader method

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class test007 extends HttpServlet

{

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

{

long expires = new Date().getTime() + 1000*60*60*24;

response.setContentType(“text/html”);

response.addDateHeader(“Expires”, expires);

ServletOutputStream out = response.getOutputStream();

out.println(“<html><head><title>Servlet Response</title></head><body>”);

out.println(“<h1>Todays News</h1>”);

out.println(getNews());

out.println(“<p><hr>”);

out.println(“<i>This news item expires “ + new Date(expires));

out.println(“</body></html>”);

}

 

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

{

response.sendError(response.SC_METHOD_NOT_ALLOWED, “Your Error Mesage”);

}

 

private String getNews()

{

return “Nothing has happened since yesterday…”;

}

}

Output

add data header

Pr.Pg border                                              Next Pg