Printing HTTP Headers in jsp files in Java Web Application.

A complete maven example, as usual.

This is what my directory layout looks like:


Ok now show the code:
pom.xml and web.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>biz.tugay</groupId>
    <artifactId>janfourteentwo</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>janfourteentwo Maven Webapp</name>
 
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>janfourteentwo</finalName>
    </build>
 
</project>

<?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">
 
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <scripting-invalid>true</scripting-invalid>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html;charset=UTF-8</default-content-type>
        </jsp-property-group>
    </jsp-config>
 
    <session-config>
        <session-timeout>30</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
        </cookie-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>
 
</web-app>

And the Servlet I have:
/* User: koray@tugay.biz Date: 14/01/16 Time: 14:33 */
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
 
@WebServlet(urlPatterns = "")
public class ControllerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        final Set<String> httpRequestHeaders = new HashSet<String>();
 
        final Enumeration<String> httpRequestHeaderNames = req.getHeaderNames();
        while (httpRequestHeaderNames.hasMoreElements()) {
            final String httpRequestHeaderName = httpRequestHeaderNames.nextElement();
            final String httpRequestHeaderValue = req.getHeader(httpRequestHeaderName);
            httpRequestHeaders.add("<strong>" + httpRequestHeaderName + "</strong>:" + httpRequestHeaderValue);
        }
        req.setAttribute("httpRequestHeaders", httpRequestHeaders);
        req.getRequestDispatcher("/main.jsp").forward(req, resp);
    }
}

And the jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Http Request Headers!</title>
</head>
<body>
<div style="margin: 20px">
    <%--@elvariable id="httpRequestHeaders" type="java.util.Set<java.lang.String>"--%>
    <c:forEach items="${httpRequestHeaders}" var="httpRequestHeader">
        <c:out value="${httpRequestHeader}" escapeXml="false"/>
        <br/>
    </c:forEach>
</div>
</body>
</html>

And the output will be something like this:


And if we refresh the same page, this time the server will get a cookie together with the request:


And maybe you want to see the Response Headers as well:


The content-type response header’s value is known as a MIME type. The MIME type tells the browser what kind of data the browser is about to receive so that the browser will know how to render it.