Solved

request header

Posted on 2014-02-23
3
265 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now