Hello, I m new to servlets. I have written a servlet program which reads http header of local host! How to read http header of any website say www.google.com?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Header;

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;

/**
 *
 * @author Andy
 */
public class HeaderServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
             //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet HeaderServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<table>");
            out.println("<h2>Request Details<h2>");
            out.println("<h5><tr><td>Content Type: </h5>"+request.getContentType());
            out.println("<h5><tr><td>Local Name: </h5>"+request.getLocalName());
            out.println("<h5><tr><td>Request URI: </h5>"+request.getRequestURI());
            out.println("<h5><tr><td>Local Port: </h5>"+request.getLocalPort());
            out.println("<h5><tr><td>Session Type: </h5>"+request.getSession());
            out.println("<h5><tr><td>Path Info: </h5>"+request.getPathInfo());
            out.println("<h5><tr><td>Request URL: </h5>"+request.getRequestURL());
            out.println("<h5><tr><td>Content Type: </h5>"+request.getContentType());


            out.println("<h1>Servlet HeaderServlet at " + request.getContextPath () + "</h1>");
       Enumeration headerNames = request.getHeaderNames();
    while(headerNames.hasMoreElements()) {
      String headerName = (String)headerNames.nextElement();
      out.println("<TR><TD>" + headerName);
      out.println("    <TD>" + request.getHeader(headerName));
    }      //out.println("<TR><TD>" + headerName);
     // out.println("<TD>" + request.getHeader());
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");


        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Help me

For that you have to establish http connection with a site and when you send a request to that site it will return response and by reading the response header you can get the http header of any site you want.

How to establish a http connection? please help I m new to jsp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.