Getting all the Header Attributes

Getting all the Header Attributes


Posted in : Java Posted on : May 25, 2012 at 6:52 PM Comments : [ 0 ]

In this servlet tutorial you will learn hoe to get all servlet headers from servlet

Getting all the Header Attributes

Hi Following is the code to get all the header attributes

Enumeration headers = request.getHeaderNames();

This method returns all the header names. By using this header names you can get heade as

	while (headers.hasMoreElements()) {
		String header = headers.nextElement();
		out.println("<br>" + header + "-  " + request.getHeader(header));
	}

The complete Servlet code is given below

HeadersExample.java

package devmanuals.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeadersExample extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		Enumeration headers = request.getHeaderNames();
		out.println("Here is the all header Names
"); while (headers.hasMoreElements()) { String header = headers.nextElement(); out.println("
" + header + "- " + request.getHeader(header)); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

You can also download the code here.

The following would be the output when you will run the above code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics