Find us on Google+ Kill the code: Program to Print Header Information in Servlet

Thursday, 2 August 2012

Program to Print Header Information in Servlet


JSP File : 


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <form action="header_info.do">
            <input type="submit" value="Go" />
        </form>
    </body>
</html>

Servlet File : 



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 header_info extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
             Enumeration one = request.getHeaderNames();
           
             while (one.hasMoreElements())
             {
                 String one1 = one.nextElement().toString();
                 out.println(one1 + "="+request.getHeader(one1)  +"<br>");
             }
           
           
        } finally {          
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}


No comments:

Post a Comment