PrintHeader.java Source code

Java tutorial

Introduction

Here is the source code for PrintHeader.java

Source

/*
 * <servlet> <servlet-name>PrintHeader </servlet-name>
 * <servlet-class>PrintHeader </servlet-class> </servlet>
 * 
 * <servlet-mapping> <servlet-name>PrintHeader </servlet-name>
 * <url-pattern>/PrintHeader </url-pattern> </servlet-mapping>
 * 
 *  
 */

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 PrintHeader extends HttpServlet {
    /**
     * doGet implementation, calls printHeader function
     * 
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     *  
     */

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        printHeader(request, response);
    }

    /**
     * doPost implementation, calls printHeader function
     * 
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     *  
     */

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        printHeader(request, response);
    }

    /**
     * Prints client header information that is available
     * 
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     *  
     */

    public void printHeader(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        String headers = null;
        String htmlHeader = "<HTML><HEAD><TITLE> Request Headers</TITLE></HEAD><BODY>";
        String htmlFooter = "</BODY></HTML>";

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        Enumeration e = request.getHeaderNames();

        out.println(htmlHeader);
        out.println("<TABLE ALIGN=CENTER BORDER=1>");
        out.println("<tr><th> Header </th><th> Value </th>");

        while (e.hasMoreElements()) {
            headers = (String) e.nextElement();
            if (headers != null) {
                out.println("<tr><td align=center><b>" + headers + "</td>");
                out.println("<td align=center>" + request.getHeader(headers) + "</td></tr>");
            }
        }
        out.println("</TABLE><BR>");
        out.println(htmlFooter);

    }
}