Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

request header

Posted on 2014-02-23
3
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

618 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