In this tutorial I will show you how to retrieve various data from a Servlet Request object, like headers, parameters, paths and session data
When the client (browser) makes a GET, POST, PUT etc. request to a Servlet the HttpServletRequest
object holds valuable information about the client and the request itself. In following example I will list some of the most interesting methods of the request object and how to extract the information.
package net.javatutorial.tutorials; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletInfo extends HttpServlet { private static final long serialVersionUID = -2383814320847175129L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pr = response.getWriter(); pr.println("=== Paths ===\n"); pr.println("Request URL : " + request.getRequestURL()); pr.println("Request URI : " + request.getRequestURI()); pr.println("Servlet path : " + request.getServletPath()); pr.println("\n=== Headers ===\n"); Enumeration<String> e = request.getHeaderNames(); while(e.hasMoreElements()){ String param = (String) e.nextElement(); pr.println(param + " : " + request.getHeader(param)); } pr.println("\n=== Parameters ===\n"); Map<String, String[]> paramsMap = request.getParameterMap(); for (String key : paramsMap.keySet()) { pr.println(key + " : " + request.getParameter(key)); } pr.println("\n=== Session ===\n"); // returns 0:0:0:0:0:0:0:1 if executed from localhost pr.println("Client IP address : " + request.getRemoteAddr()); pr.println("Session ID : " + request.getRequestedSessionId()); // Cookie objects the client sent with this request Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { pr.print(cookie.getName() + ";"); } } } }
Requesting the servlet on localhost
Tomcat server at http://localhost:8080/ServletInfo/info?section=tutorials&lang=java from my Google Chrome Version 51.0.2704.103 displays following output:
HttpServletRequest Methods
getRequestURL() – Returns the entire URL that the client used to make the request, including protocol (http or https), server name, port number, and server path but not including the query parameters
getRequestURI() – returns only the server path part of the URL
getServletPath() – returns application path as deployed in Tomcat (in the above example this is /info)
getHeaderNames() – returns a enumeration of the names off all headers passed with the request
getHeader(headerName) – returns the value of a header with the given name
getParameterMap() – returns a HashMap with all parameters and their values
getParameter(key) – returns the value of a parameter with given key(name)
getRemoteAddr() – returns the IP address of the client. If the request is executed on localhost the return value is 0:0:0:0:0:0:0:1 or the IPv6 equivalent of 127.0.0.1
getRequestedSessionId() – returns remote session id, if provided
getCookies() – returns an array of Cookie objects the client sent with this request
You can find the entire project in GitHub : https://github.com/JavaTutorialNetwork/Tutorials/tree/master/ServletInfo