Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 :



 
© 2021 Learn Java by Examples Template by Hubberspot