728x90 AdSpace

Saturday, 29 March 2014

Servlet – set Header values in Response

In this tutorial we are going to see how to set header information in servlet response with example program. Click here to read more about HTTP/1.1 Header param with Definitions
List of Response Headers
Allow This header specifies the request method that server support – GET,POST,etc
Cache-Control Values of this header are public,private,no-cache,etc
public –
Document is cacheable
private – Document for single user
no-cache – Document should not be cached
Content-Disposition The browser ask the user to save the response to disk.
Content-Encoding This specifies the way in which page was encoded
Content-Language This specifies the language which the document written.
Content-Length This specifies the number of bytes in the response.
Content-Type This specifies MIME type of response document
Expires This specifies the time at which the content can be considered as out of date.
Last-Modified This specifies when the document was last changed.
Location This notifies the browser of the document address. The browser automatically reconnects to this location and retrieves the new document.
Refresh Used to specify the time which the page get refreshed.
Retry-After This header can be used in conjunction with a 503  response to tell the client how soon it can repeat its request.
Set-Cookie This specifies cookie associated with the page
Connection This header instructs the browser whether to use persistent in HTTP connections or not. A value of close instructs the browser not to use persistent HTTP connections and keep-alive means using persistent connections.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called HttpResponseHeader as shown in figure.
Create Servlet
http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.
http servlet Services
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
    <servlet-name>Your Servlet Name</servlet-name> 
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class> 
    <servlet-name>Your Servlet Name</servlet-name> 
    <url-pattern>/URL to Call Servlet</url-pattern> 


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

8. Add the required code inside doGet() method.


package com.javatutorialscorner.servlet;

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;

 * Servlet implementation class HttpResponseHeader
public class HttpResponseHeader extends HttpServlet {
 private static final long serialVersionUID = 1L;

  * @see HttpServlet#HttpServlet()
 public HttpResponseHeader() {
  // TODO Auto-generated constructor stub

  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setIntHeader("Refresh", 5);
  PrintWriter writer = response.getWriter();
  Date date = new Date();
  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet Response Header</title>"
    + "</head><body><h1>Java Tutorials Corner - Servlet Response Header</h1><table><tr>"
    + "<td>Current Date & Time  : </td><td>"
    + date
    + "</td></tr></table></body></html>");

  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub


9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 

10.call the URL which is mapped in web.xml.


see the page get refresh every 5 seconds.


If you call the URL with Live HTTP header plug-in. see can see the following content in header.


GET /ServletExample/HttpResponseHeader HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
refresh: 5
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 425
Date: Sun, 30 Mar 2014 02:27:54 GMT

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Servlet – set Header values in Response Rating: 5 Reviewed By: Annamalai Thangaraj