How can you read a request header information in JSP?

JSPJava 8Object Oriented ProgrammingProgramming

Following is the example which uses getHeaderNames() method of HttpServletRequest to read the HTTP header information. This method returns an Enumeration that contains the header information associated with the current HTTP request.

Once we have an Enumeration, we can loop down the Enumeration in the standard manner. We will use the hasMoreElements() method to determine when to stop and the nextElement() method to get the name of each parameter name.

<%@ page import = "*,java.util.*" %>

      <title>HTTP Header Request Example</title>
         <h2>HTTP Header Request Example</h2>
         <table width = "100%" border = "1" align = "center">
            <tr bgcolor = "#949494">
               <th>Header Name</th>
               <th>Header Value(s)</th>
               Enumeration headerNames = request.getHeaderNames();
               while(headerNames.hasMoreElements()) {
                  String paramName = (String)headerNames.nextElement();
                  out.print("<tr><td>" + paramName + "</td>
");                   String paramValue = request.getHeader(paramName);                   out.println("<td> " + paramValue + "</td></tr>
");                }             %>          </table>       </center>    </body> </html>
Updated on 30-Jul-2019 22:30:25