Wednesday, 8 October 2014

Displaying the values of request header in tabular form



package RequestHeaderPkg;

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 RequestHeaderCls
 */
@WebServlet("/RequestHeaderCls")
public class RequestHeaderCls extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestHeaderCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            PrintWriter pw=response.getWriter();
            String str;
            Enumeration hnames=request.getHeaderNames();
            String rhn,rhv;//request header name and request header value;
           
            str=  "<html>"+
                              "<body>"+
                                    "<h1><marquee>Request Header</marquee></h1>"+
                                    "<table>"+
                                          "<th>Header Name</th><th>Header Value</th>"+
                                          "<tr>"+
                                                "<td>Method : </td>"+
                                                "<td>"+request.getMethod()+"</td>"+
                                          "</tr>"+
                                          "<tr>"+
                                                "<td>Protocol : </td>"+
                                                "<td>"+request.getProtocol()+"</td>"+
                                          "</tr>"+
                                          "<tr>"+
                                                "<td>URI : </td>"+
                                                "<td>"+request.getRequestURI()+"</td>"+
                                          "</tr>"+
                                    "</table>"+
                                    "<table border='5'>"+
                                    "<th>Header Name</th><th>Header Value</th>";

            while(hnames.hasMoreElements())
            {
                  rhn=(String)hnames.nextElement();
                  rhv=request.getHeader(rhn);
                  str+= "<tr>"+
                                    "<td>"+rhn+"</td>"+
                                    "<td>"+rhv+"</td>"+
                              "</tr>";
            }
                             
            str+="</table>"+"</body>"+
                        "</html>";
            pw.println(str);
           
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

No comments:

Post a Comment