Solved

request header

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
jmss example java 2 33
servlet and mdb, jms error 1 57
Is Applet the way to go for my drag and drop system? 8 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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