Information from "request" Object

This section provides a tutorial example on how to obtain information from the 'request' implicit object provided by the JSP container.

The "request" object is an important implicit object to the JSP page, because it contains a lot of useful information. The following JSP page will you some details of the "request" object:

<!--
 - RequestInfo.jsp
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<html><body>
<pre>
Information about request:
<% 
   out.println("Class Name: "+request.getClass().getName());
   out.println("Auth Type: "+request.getAuthType());
   out.println("Context Path: "+request.getContextPath());
   out.println("Method: "+request.getMethod());
   out.println("Path Info: "+request.getPathInfo());
   out.println("Path Translated: "+request.getPathTranslated());
   out.println("Query String: "+request.getQueryString());
   out.println("Remote User: "+request.getRemoteUser());
   out.println("Session ID: "+request.getRequestedSessionId());
   out.println("Request URI: "+request.getRequestURI());
   out.println("Request URL: "+request.getRequestURL());
   out.println("Servlet Path: "+request.getServletPath());
   out.println("Cookies:");
   javax.servlet.http.Cookie[] cookies = request.getCookies();
   if (cookies!=null) {
      for (int i=0; i<cookies.length; i++) {
         out.println("   "+cookies[i].getName()+": "
            +cookies[i].getValue());
      }
   }
   out.println("Headers:");
   java.util.Enumeration e = request.getHeaderNames();
   while (e.hasMoreElements()) {
      String n = (String) e.nextElement();
      out.println("   "+n+": "+request.getHeader(n));
   }
%>
</pre>
</body></html>

Deploy it on the Tomcat 7 server and visit it with a browser to see the output:

Information about request:
Class Name: org.apache.catalina.connector.RequestFacade
Auth Type: null
Context Path: 
Method: GET
Path Info: null
Path Translated: null
Query String: null
Remote User: null
Session ID: B543957CB66D707E234B0808A989130D
Request URI: /RequestInfo.jsp
Request URL: http://localhost:8080/RequestInfo.jsp
Servlet Path: /RequestInfo.jsp
Cookies:
   JSESSIONID: B543957CB66D707E234B0808A989130D
Headers:
   host: localhost:8080
   user-agent: Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101
      Firefox/9.0.1
   accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;
      q=0.8
   accept-language: en-us,en;q=0.5
   accept-encoding: gzip, deflate
   accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
   dnt: 1
   connection: keep-alive
   cookie: JSESSIONID=B543957CB66D707E234B0808A989130D

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

JSP Implicit Objects

 What Are Implicit Objects

 Information of JSP Execution Context

Information from "request" Object

 Methods Supported by the "session" Object

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version