Pin It

How to display HTTP Request Headers through a Servlet ?.

Program to display HTTP Request Headers through a Servlet ?

package com.hubberspot.jsp.servlets.examples;

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;

@WebServlet("/RequestHeadersServlet")
public class RequestHeadersServlet extends HttpServlet {
   
 protected void doGet(
   HttpServletRequest request,
   HttpServletResponse response)
   throws ServletException, IOException
 {
  
  doPost(request, response);
    
 }

 
 protected void doPost(
   HttpServletRequest request, 
   HttpServletResponse response) 
   throws ServletException, IOException 
 {
  Enumeration<String> headers = request.getHeaderNames();
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
  out.print("<html>");
  out.print("<body>");
  out.print("<h1>The Request Headers are as follows : </h1>");
  out.print("<table border='1'>");  
  
  while(headers.hasMoreElements())
  {
   String header = headers.nextElement();
   String headerValue = request.getHeader(header);
   out.print("<tr>");
   out.print("<td>");
   out.print(header);
   out.print("</td>");
   out.print("<td>");
   out.print(headerValue);   
   out.print("</td>");
   out.print("</tr>");
  }
  out.print("</table>");
  out.print("</body>");  
  out.print("</html>");

 }
}




Output of the program :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot