In this servlet tutorial you will learn hoe to get all servlet headers from servlet
Getting all the Header Attributes
Hi Following is the code to get all the header attributes
Enumerationheaders = request.getHeaderNames();
This method returns all the header names. By using this header names you can get heade as
while (headers.hasMoreElements()) { String header = headers.nextElement(); out.println("<br>" + header + "- " + request.getHeader(header)); }
The complete Servlet code is given below
HeadersExample.java
package devmanuals.servlet; 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 HeadersExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Enumerationheaders = request.getHeaderNames(); out.println("Here is the all header Names
"); while (headers.hasMoreElements()) { String header = headers.nextElement(); out.println("
" + header + "- " + request.getHeader(header)); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
You can also download the code here.
The following would be the output when you will run the above code
[ 0 ] Comments