title: Java Servlet Download
body: <p>I have wrote a servlet which is used to check http header but I don't know why when the page is loaded, it starts downloading automatically. </p>
  
  <pre><code>    /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  // ===================================================
  package HttpHeader;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import java.util.Enumeration;
  import java.util.zip.GZIPOutputStream;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletContext;
  
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.Cookie;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  // ===================================================
  public class HeaderServlet extends HttpServlet {
  
      private static final long serialVersionUID = 1234L;
  
      private ServletConfig m_ServletConfig;
      private ServletContext m_ServletContext;
  
      private boolean m_HasCompress;
  
      private int m_ContentLength;
  
      private Cookie[] m_Cookies;
      private String m_AuthenticationType;
      private String m_RemoteUser;
      private String m_ContentType;
  
      private String m_Method;
      private String m_RequestURI;
      private String m_Date;
      private String m_QueryStr;
      private String m_Protocol;
  
      private Enumeration&lt;String&gt; m_HeaderNames;
  
      private HttpSession m_Session;
      //======================================================
  
      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("&lt;html&gt;");
              out.println("&lt;head&gt;");
              out.println("&lt;title&gt;Servlet HeaderServlet&lt;/title&gt;");
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
              out.println("&lt;h1&gt;Servlet HeaderServlet at " + request.getContextPath () + "&lt;/h1&gt;");
              out.println("&lt;/body&gt;");
              out.println("&lt;/html&gt;");
              */
          } finally {
              out.close();
          }
      }
  
      @Override
      protected void doHead(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
  
  
          String encoding = request.getHeader("Accept-Header");
          if(encoding != null &amp;&amp; encoding.indexOf("gzip") != -1)
          {
            m_HasCompress = true;
          }
          else
          {
             m_HasCompress = false;
          }
  
          m_ContentLength = request.getContentLength();
  
          m_Cookies = request.getCookies();
  
          // Basic, Form, Client certification authentication,
          // digest authentication
          // Same with CGI AUTH_TYPE
          m_AuthenticationType = request.getAuthType();
  
          m_RemoteUser = request.getRemoteUser();
  
          m_ContentType = request.getContentType();
  
          // Return get, post, delete, put
          // Same with CGI REQUEST_METHOD
          m_Method = request.getMethod();
  
          m_RequestURI = request.getRequestURI();
  
          // Same with CGI QUERY_STRING
          m_QueryStr = request.getQueryString();
  
          // Same with CGI SERVER_PROTOCOL
          m_Protocol = request.getProtocol();
  
          m_HeaderNames = request.getHeaderNames();
  
          m_Session = request.getSession(true);
  
          /*  Accept
           *  = MIME type
           *
           *  Accept-Charset
           *  = UTF-8
           *
           *  Accpet-Encoding
           *  = Compression - gzip
           *
           *  Accept-Language
           *  = us
           *
           *  Authorization
           *  = Login
           *
           *  Content-Length
           *  = Post request
           *
           *  Cookie
           *  Host
           *
           *  If-Modified-since
           *  = Implements getLastModified
           *  = Get request
           *
           *  If-Unmodified-since
           *  = Post request
           *
           *  Referer
           *  = Request origin
           *
           *  User Agent
           *  = Mozilla
           */
      }
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
          PrintWriter out = null;
          try {
  
              doHead(request, response);
  
              response.setContentType("/text/html;charset=UTF-8");
              response.setBufferSize(8192);
  
              if(m_HasCompress)
              {
                out = new PrintWriter(new GZIPOutputStream(response.getOutputStream()));
                response.setHeader("Content-Encoding", "gzip");
              }
              else
              {
                out = response.getWriter();
              }
  
              if (out != null) {
  
                  String docType = "&lt;!DOCTYPE HTML PUBLIC "
                          + "\"-//W3C//DTD HTML 4.01 "
                          + "Transitional//EN\"&gt;\n\n";
  
                  out.println(docType
                          + "&lt;html&gt; &lt;head&gt; "
                          + " &lt;title&gt; Http Request Header&lt;title&gt;"
                          + "Requst Method : " + m_Method + "&lt;p&gt;&lt;/p&gt;"
                          + "Request URI : " + m_RequestURI + "&lt;p&gt;&lt;/p&gt;"
                          + "Request Protocol : " + m_Protocol + "&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;"
                          + "&lt;table BORDER=1 ALIGN =\"CENTER\"&gt;"
                          + "&lt;TH&gt;Header Name &lt;TH&gt;Header Value");
  
                  String headerName = new String();
                  String headerValue = new String();
                  while(m_HeaderNames.hasMoreElements())
                  {
                    headerName = m_HeaderNames.nextElement();
                    if(headerName != null)
                    {
                       headerValue = request.getHeader(headerName);
  
                       out.println("&lt;TR&gt;&lt;TD&gt;" + headerName);
                       out.println("    &lt;TD&gt;" + headerValue);
                    }
                  }
  
                  out.println("&lt;/table&gt;" + "&lt;p&gt;&lt;/p&gt;"
                          + "&lt;/head&gt;&lt;/html&gt;");
                  out.close();
  
              }
  
          }
          finally {
  
              out.close();
          }
  
  
  
           /*
           * HTTP 1.1 request headers
           * Request Header set by browser
           * Get Reqeust - Query Data same line
           * POst Request - Query Data next line
           *
           */
  
          //processRequest(request, response);
      }
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  //        processRequest(request, response);
      }
  
      @Override
      public String getServletInfo() {
          return "Short description";
      }// &lt;/editor-fold&gt;
  
  }
  // ===================================================
  
  
  
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  
  &lt;%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%&gt;
  &lt;%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%&gt;
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;%--
      This file is an entry point for JavaServer Faces application.
  --%&gt;
  &lt;f:view&gt;
      &lt;html&gt;
          &lt;head&gt;
              &lt;meta http-equiv="REFRESH" content="0; url=HeaderServlet; charset=UTF-8"/&gt;
              &lt;title&gt;JSP Page&lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
              &lt;h1&gt;&lt;h:outputText value="JavaServer Faces"/&gt;&lt;/h1&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  &lt;/f:view&gt;
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" 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"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Development&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;HeaderServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;HttpHeader.HeaderServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;HeaderServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/HeaderServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;faces/welcomeJSF.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>The URL loaded is <a href="http://localhost:8080/HttpHeader/" rel="nofollow">http://localhost:8080/HttpHeader/</a>. 
  I try to debug but never found anything weird. 
  Please help me. </p>
  
  <p>Thanks. </p>