Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4231954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:02:03+00:00 2026-05-21T02:02:03+00:00

I have wrote a servlet which is used to check http header but I

  • 0

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.

    /*
 * 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<String> 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("<html>");
            out.println("<head>");
            out.println("<title>Servlet HeaderServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet HeaderServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally {
            out.close();
        }
    }

    @Override
    protected void doHead(HttpServletRequest request, HttpServletResponse response)
                   throws ServletException, IOException {


        String encoding = request.getHeader("Accept-Header");
        if(encoding != null && 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 = "<!DOCTYPE HTML PUBLIC "
                        + "\"-//W3C//DTD HTML 4.01 "
                        + "Transitional//EN\">\n\n";

                out.println(docType
                        + "<html> <head> "
                        + " <title> Http Request Header<title>"
                        + "Requst Method : " + m_Method + "<p></p>"
                        + "Request URI : " + m_RequestURI + "<p></p>"
                        + "Request Protocol : " + m_Protocol + "<p></p><p></p>"
                        + "<table BORDER=1 ALIGN =\"CENTER\">"
                        + "<TH>Header Name <TH>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("<TR><TD>" + headerName);
                     out.println("    <TD>" + headerValue);
                  }
                }

                out.println("</table>" + "<p></p>"
                        + "</head></html>");
                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";
    }// </editor-fold>

}
// ===================================================



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%--
    This file is an entry point for JavaServer Faces application.
--%>
<f:view>
    <html>
        <head>
            <meta http-equiv="REFRESH" content="0; url=HeaderServlet; charset=UTF-8"/>
            <title>JSP Page</title>
        </head>
        <body>
            <h1><h:outputText value="JavaServer Faces"/></h1>
        </body>
    </html>
</f:view>

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>HeaderServlet</servlet-name>
        <servlet-class>HttpHeader.HeaderServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>HeaderServlet</servlet-name>
        <url-pattern>/HeaderServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/welcomeJSF.jsp</welcome-file>
    </welcome-file-list>
</web-app>

The URL loaded is http://localhost:8080/HttpHeader/.
I try to debug but never found anything weird.
Please help me.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:02:03+00:00Added an answer on May 21, 2026 at 2:02 am

    change

    response.setContentType("text/html;charset=UTF-8");
    //instead of: 
    //response.setContentType("/text/html;charset=UTF-8");
    

    Small adivce:
    Remember about stateless nature of servlet – servlets are singletons. It’s mean that, in your case, if 2 clients call your servlet, result’ll be unpredictable. This is because you’ve many fields which should be specific per client, actually those fields could be modify incorrectly, because of concurrently call to the Servlet: For one client it’ll be work, For more than one your servlet can be broken – change your implementatio if you have more than one client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTTP server which is in our internal network and accessible only
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I have a controller servlet which forward the request to the model servlet.Now when
I have the following code which handles files upload on the server. But how
I have a REST web service class which i call HttpRequest using curl.I wrote
Unix gurus! I have a Java program which passes some parameters to a Servlet
I have this Array i wrote a function MostFreq that takes an array of
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I have a Python script I recently wrote that I call using the command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.