Servlets Server HTTP Response

As examined in the past section, when a Web server reacts to a HTTP ask for, the reaction ordinarily comprises of a status line, some reaction headers, a clear line, and the record. A run of the mill reaction resembles this −

HTTP/1.1 200 OK 

Content-Type: content/html 

Header2: ... 


HeaderN: ... 

(Clear Line) 

<!doctype ...> 







The status line comprises of the HTTP rendition (HTTP/1.1 in the model), a status code (200 in the precedent), and an extremely short message relating to the status code (OK in the model).

Following is a synopsis of the most valuable HTTP 1.1 reaction headers which return to the program from web server side and you would utilize them in all respects as often as possible in web programming −

Sr.No. Header & Description


This header determines the demand techniques (GET, POST, and so on.) that the server supports.



This header indicates the conditions in which the reaction report can securely be reserved. It can have values public, private or no-cache and so forth. Open methods report is cacheable, Private methods archive is for a solitary client and must be put away in private (non-shared) stores and nocache implies record ought to never be cached.



This header trains the program whether to utilize persevering in HTTP associations or not. An estimation of close teaches the program not to utilize tenacious HTTP associations and keepalive implies utilizing diligent connections.



This header gives you a chance to ask for that the program request that the client spare the reaction to plate in a record of the given name.



This header indicates the manner by which the page was encoded amid transmission.



This header implies the language in which the report is composed. For instance en, en-us, ru, etc



This header shows the quantity of bytes in the reaction. This data is required just if the program is utilizing a steady (keep-alive) HTTP connection.



This header gives the MIME (Multipurpose Internet Mail Extension) sort of the reaction document.



This header determines the time at which the substance ought to be considered obsolete and in this way never again be cached.



This header demonstrates when the archive was last changed. The customer would then be able to reserve the record and supply a date by a If-Modified-Since ask for header in later requests.



This header ought to be incorporated with all reactions that have a status code during the 300s. This advises the program of the archive address. The program consequently reconnects to this area and recovers the new document.



This header indicates how soon the program ought to request a refreshed page. You can indicate time in number of seconds after which a page would be refreshed.



This header can be utilized related to a 503 (Service Unavailable) reaction to tell the customer how soon it can rehash its request.



This header indicates a treat related with the page.

Methods to Set HTTP Response Header

There are following strategies which can be utilized to set HTTP reaction header in your servlet program. These techniques are accessible with HttpServletResponse object.

Sr.No. Method & Description

String encodeRedirectURL(String url)

Encodes the predetermined URL for use in the sendRedirect technique or, if encoding isn't required, restores the URL unchanged.


String encodeURL(String url)

Encodes the predefined URL by incorporating the session ID in it, or, if encoding isn't required, restores the URL unchanged.


boolean containsHeader(String name)

Returns a Boolean demonstrating whether the named reaction header has just been set.


boolean isCommitted()

Returns a Boolean demonstrating if the reaction has been committed.


void addCookie(Cookie cookie)

Adds the predetermined treat to the response.


void addDateHeader(String name, long date)

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


void addHeader(String name, String value)

Adds a reaction header with the given name and value.


void addIntHeader(String name, int value)

Adds a reaction header with the given name and number value.


void flushBuffer()

Forces any substance in the cradle to be composed to the client.


void reset()

Clears any information that exists in the cushion just as the status code and headers.


void resetBuffer()

Clears the substance of the fundamental support in the reaction without clearing headers or status code.


void sendError(int sc)

Sends a blunder reaction to the customer utilizing the predetermined status code and clearing the buffer.


void sendError(int sc, String msg)

Sends a blunder reaction to the customer utilizing the predetermined status.


void sendRedirect(String location)

Sends an impermanent divert reaction to the customer utilizing the predetermined divert area URL.


void setBufferSize(int measure)

Sets the favored support estimate for the body of the response.


void setCharacterEncoding(String charset)

Sets the character encoding (MIME charset) of the reaction being sent to the customer, for instance, to UTF-8.


void setContentLength(int len)

Sets the length of the substance body in the reaction In HTTP servlets, this strategy sets the HTTP Content-Length header.


void setContentType(String type)

Sets the substance sort of the reaction being sent to the customer, if the reaction has not been submitted yet.


void setDateHeader(String name, long date)

Sets a reaction header with the given name and date-esteem.


void setHeader(String name, String esteem)

Sets a reaction header with the given name and value.


void setIntHeader(String name, int esteem)

Sets a reaction header with the given name and whole number value


void setLocale(Locale loc)

Sets the region of the reaction, if the reaction has not been submitted yet.


void setStatus(int sc)

Sets the status code for this response

HTTP Header Response Example

You as of now have seen setContentType() technique working in past precedents and following model would likewise utilize same strategy, also we would utilize setIntHeader() technique to set Refresh header.

//Import required java libraries 


import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Extend HttpServlet class 

public class Refresh extends HttpServlet { 

//Method to deal with GET strategy request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set invigorate, autoload time as 5 seconds 

response.setIntHeader("Refresh", 5); 

//Set reaction content type 


//Get present time 

Calendar schedule = new GregorianCalendar(); 

String am_pm;
int hour = calendar.get(Calendar.HOUR); 

int minute = calendar.get(Calendar.MINUTE); 

int second = calendar.get(Calendar.SECOND); 

if(calendar.get(Calendar.AM_PM) == 0) 

am_pm = "AM"; 


am_pm = "PM"; 

String CT = hour+":"+ minute +":"+ second +" "+ am_pm; 

PrintWriter out = response.getWriter(); 

String title = "Auto Refresh Header Setting"; 

String docType = 

"<!doctype html open \"-/w3c//dtd html 4.0 " + "transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n"+ 

"<body bgcolor = \"#f0f0f0\">\n" + 

"<h1 adjust = \"center\">" + title + "</h1>\n" + 

"<p>Current Time is: " + CT + "</p>\n" 



//Method to deal with POST technique request. 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 



Now calling the above servlet would show current framework time after like clockwork as pursues. Simply run the servlet and hold on to see the outcome −

Auto Refresh Header Setting

Current Time is: 9:44:50 PM

© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3