Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Header Information available from the client in Servlet 
 

In this section you will studied how to display the header information in servlet.

 

Header Information available from the client in Servlet

                         

In this section you will studied how to display the header information in servlet.

When a HTTP client sends a request, generally GET or POST method is specified. It can send a number of headers. Here are some headers:

Accept - MIME type, the browser prefers.
Accept-Charset - The Character set the browser expects.
Accept-Language - The language which the browser is accepting.
Connection - If a servlet gets a keep-Alive value or gets a request line indicating HTTP 1.1, it may be able to take advantage of connection.
Host (host and port as defined in url).
Content-Length (for POST messages).
User-Agent (type of browser, if servlet is returning browser-specific content).

In the given example, method getHeaderNames() is called  to get an Enumeration of all header names received on this particular request. The getMethod() returns the main request method (GET or POST, but HEAD, PUT, DELETE are also possible). The getRequestURI() method returns the URI (the part of url that came after the host and port , but before the form data). The getRequestProtocol() returns the protocol which is generally  HTTP/1.0 or HTTP/1.1. Using while loop, all the headers are passed into the header. Now if header is not equal to null, all the header information will be displayed by using the method req.getHeader().

Here is the code of HeaderInformation.java

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

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

public class HeaderInformation extends HttpServlet {
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws IOException, ServletException {
  printHeader(req, res);
  }
  public void doPost(HttpServletRequest req, HttpServletResponse res)
  throws IOException, ServletException {
  printHeader(req, res);
  }
  public void printHeader(HttpServletRequest req,
  HttpServletResponse res) throws IOException, ServletException {
  String header = null;
  String head = "<html><head><title> Header Information</title>
  </head><body>"
;
  String foot = "</body></html>";
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  Enumeration enumeration = req.getHeaderNames();
  pw.println(head);
  pw.println("<h2> Header Information </h2>");
  pw.println("<B>Request Method: </B>" +
                req.getMethod() + "<BR>\n" +
                "<B>Request URI: </B>" +
                req.getRequestURI() + "<BR>\n" +
                "<B>Request Protocol: </B>" +
                req.getProtocol() + "<BR><BR>\n" );

  pw.println("<table>");
  while (enumeration.hasMoreElements()) {
      header = (String) enumeration.nextElement();
      if (header != null) {
        pw.println("<tr><td><b>" + header + "</td>");
        pw.println("<td >" + req.getHeader(header)
            "</td></tr>");
      }
    }
    pw.println("</table><br>");
    pw.println(foot);

  }
}

After compiling the servlet, place it to the appropriate place in the Tomcat. Run the tomcat. Open the browser and type the url http://localhost:8080/Myexamples/HeaderInformation

 Following output will be displayed.

Download Source Code

                         
» View all related tutorials
Related Tags: c ide orm table data form io make type column field name columns change this id nsis war tab row

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.