GTU WTAD Practical 11 : Write a Servlet to display all the headers available from request.

GTU WTAD Practical 11 : Write a Servlet to display all the headers available from request.

GTU WTAD Practical 11

Hope you have enjoyed previous article and successfully executed your first servlet program. If not you can refer it first how to start with servlet. In this program all the headers available in request object. Header is very useful to handle server side code. like from where request has came and ip address of the user this Request Header is also used to get used to generate different logs.

Jump to GTU WTAD Practical 12

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu11\

<%
	response.sendRedirect("./AvailableHeaders");
%>

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu_11\WEB-INF\

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 11 </display-name>
  <description>  Write a Servlet to display all the headers available from request.  </description>
  
  <servlet>
    <servlet-name>AvailableHeaders</servlet-name>
    <servlet-class>AvailableHeaders</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AvailableHeaders</servlet-name>
      <url-pattern>/AvailableHeaders</url-pattern>
  </servlet-mapping> 
  
</web-app>

 

File Name : AvailableHeaders.java

File Location : Tomcat_Apache\webapps\gtu11\WEB-INF\classes

/*
		GTU WTAD Practical 11 : Write a Servlet to display all the headers available from request. 
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AvailableHeaders extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GTU WTAD Practical 11 : Write a Servlet to display all the headers available from request.</title></head><body><center><h1>GTU WTAD Practical 11 : Write a Servlet to display all the headers available from request.  </h1></center><br><br>");
		
		//Display Header Name and Value in Table Format 
		out.println("<table border=1>");
		out.println("<tr><th>Header Name</th><th>Header Values</th></tr>");
		Enumeration e = request.getHeaderNames();	//Fetch Header Names from Request Object and store to enumeration.
		while(e.hasMoreElements())
		{
			String header_name = (String) e.nextElement();
			String header_value = (String) request.getHeader(header_name);
			out.println("<tr><td><b>"+header_name + "</b></td><td> " + header_value + "</td></tr>");
		}
		out.println("</table>");
		
		out.println("</body></html>");
	}
}

Output

GTU WTAD Practical 11

 

 

Leave a Reply