Solved

request header

Posted on 2014-02-23
3
287 Views
Last Modified: 2014-04-03
Hi,


I am trying below example

http://www.tutorialspoint.com/servlets/servlets-client-request.htm

I got below output
Header Name Header Value(s)
accept image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-mfe-ipt, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language en-us
cache-control no-cache
ua-cpu x86
accept-encoding gzip, deflate
user-agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; McAfee; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; MS-RTC LM 8)
host localhost:8080
connection Keep-Alive



My question is
Enumeration headerNames = request.getHeaderNames();
      
      while(headerNames.hasMoreElements()) {
         String paramName = (String)headerNames.nextElement();
         out.print("<tr><td>" + paramName + "</td>\n");
         String paramValue = request.getHeader(paramName);
         out.println("<td> " + paramValue + "</td></tr>\n");
      }

Open in new window


Why we need enumeration as above.
Also is header is a map with key , value pair. How it has both name and value inside it?

My complete code is
package com.tp.first;

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

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

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String message;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    public void init() throws ServletException
    {
        // Do required initialization
        message = "Hello World";
    }

 // Method to handle GET method request.
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
              throws ServletException, IOException
    {
        // Set response content type
        response.setContentType("text/html");
   
        PrintWriter out = response.getWriter();
  	  String title = "HTTP Header Request Example";
        String docType =
        "<!doctype html public \"-//w3c//dtd html 4.0 " +
        "transitional//en\">\n";
        out.println(docType +
          "<html>\n" +
          "<head><title>" + title + "</title></head>\n"+
          "<body bgcolor=\"#f0f0f0\">\n" +
          "<h1 align=\"center\">" + title + "</h1>\n" +
          "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
          "<tr bgcolor=\"#949494\">\n" +
          "<th>Header Name</th><th>Header Value(s)</th>\n"+
          "</tr>\n");
   
        Enumeration headerNames = request.getHeaderNames();
        
        while(headerNames.hasMoreElements()) {
           String paramName = (String)headerNames.nextElement();
           out.print("<tr><td>" + paramName + "</td>\n");
           String paramValue = request.getHeader(paramName);
           out.println("<td> " + paramValue + "</td></tr>\n");
        }
        out.println("</table>\n</body></html>");
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    // Method to handle POST method request.
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
        throws ServletException, IOException {
       doGet(request, response);
    }

}

Open in new window

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 39881473
Why we need enumeration as above.

The Java EE specification is very old. It probably predates the java.util.Map interface, which probably would have been more useful. (In general, any time you see Java library code using Enumeration, you can be pretty sure that bit of code was designed a long time ago.)

Also is header is a map with key , value pair. How it has both name and value inside it?

I'm not sure what you're asking here. The HTTP header does indeed map header names to header values. The names are the key of the map and the values are the value of the map.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39954255
I got this output
Header Name Header Value(s)
accept image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-mfe-ipt, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language en-us
cache-control no-cache
ua-cpu x86
accept-encoding gzip, deflate
user-agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; McAfee; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; MS-RTC LM 8)
host localhost:8080
connection Keep-Alive

what is name and what is value here. please advise
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 39954273
For each line, the name is the string before the first space character and the value is everything after it.

So for this one:

connection Keep-Alive

The name is "connection" and the value is "Keep-Alive".
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
What's wrong with this code? 4 25
Java: anonymous class 4 29
Java program running SQL query 5 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question