Solved

request header

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
java  and programming certification ? 4 86
Impossible to extract MSI from new JAVA releases 2 47
Problem to start Neon 20 83
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

929 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

12 Experts available now in Live!

Get 1:1 Help Now