© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Displaying the Header Using Java


This program is a servlet which will retrieve the Http request header. In HttpRequest there are too many headers. To retrieve all the headers firstly we need to call the getWriter () which returns PrintWriter object and helps us to display all the headers. To get a header names call the method getHeaderNames () of the request object which will return the Enumeration of the headers. To retrieve all the headers from the Enumeration use the method hasMoreElements (). This method checks whether there are more headers or not. To display the output on your browser use the PrintWriter object.

Displaying the Header Using Java:

import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HeaderDet extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); pw.println("Request Headers are"); Enumeration enumeration = request.getHeaderNames(); while(enumeration.hasMoreElements()){ String headerName = (String)enumeration.nextElement(); Enumeration headerValues = request.getHeaders(headerName); if (headerValues != null){ while (headerValues.hasMoreElements()){ String values = (String) headerValues.nextElement(); pw.println(headerName + ": " + values); } } } } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑