Information of JSP Execution Context

This section provides a tutorial example on how to obtain information from the JSP execution context environment through 'session' and 'application' implicit object provided by the JSP container.

To interact with the JSP/Servlet container more effectively, I wrote the following JSP page to retrieve some basic information about the JSP execution environment:

<!-- ContextInfo.jsp
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<html><body>
<pre>
JSP Page Context Information
----------------------------

Pre-defined objects:
<%
   out.println("out: "+out.getClass().getName());
   out.println("this: "+this.getClass().getName());
   out.println("request: "+request.getClass().getName());
   out.println("response: "+response.getClass().getName());
   out.println("session: "+session.getClass().getName());
   out.println("application: "+application.getClass().getName()
      );
   out.println("config: "+config.getClass().getName());
   out.println("pageContext: "+pageContext.getClass().getName()
      );
%>

Information about session:
<%
   out.println("Class Name: "+session.getClass().getName());
   out.println("Session ID: "+session.getId());
   java.util.Date d = new java.util.Date();
   d.setTime(session.getCreationTime());
   out.println("Create Time: "+d.toString());
   d.setTime(session.getLastAccessedTime());
   out.println("Last Access Time: "+d.toString());
   out.println("Is Session New: "+session.isNew());
%>

Information about sessionContext:
<%
   javax.servlet.http.HttpSessionContext c
      = session.getSessionContext();
   out.println("Class name: "+c.getClass().getName());
%>

Information about application:
<%
   out.println("Class Name: "+application.getClass().getName());
   out.println("Major Version: "+application.getMajorVersion());
   out.println("Minor Version: "+application.getMinorVersion());
   out.println("Server Info: "+application.getServerInfo());
   out.println("Serlet Context Name: "
      +application.getServletContextName());
   java.util.Enumeration e = application.getServletNames();
   while (e.hasMoreElements()) {
      String n = (String) e.nextElement();
      out.println("Servlet Name: "+n);
   }
   e = application.getInitParameterNames();
   while (e.hasMoreElements()) {
      String n = (String) e.nextElement();
      out.println("Init Parameter Name: "+n);
   }
%>
</pre>
</body></html>

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

JSP Page Context Information

Pre-defined objects:
out: org.apache.jasper.runtime.JspWriterImpl
this: org.apache.jsp.ContextInfo_jsp
request: org.apache.catalina.connector.RequestFacade
response: org.apache.catalina.connector.ResponseFacade
session: org.apache.catalina.session.StandardSessionFacade
application: org.apache.catalina.core.ApplicationContextFacade
config: org.apache.catalina.core.StandardWrapperFacade
pageContext: org.apache.jasper.runtime.PageContextImpl

Information about session:
Class Name: org.apache.catalina.session.StandardSessionFacade
Session ID: 24410F3CB643883DA1E6C3E18E6B5A15
Create Time: 22:57:29 EDT 2018
Last Access Time: 22:57:29 EDT 2018
Is Session New: true

Information about sessionContext:
Class name: org.apache.catalina.session.StandardSessionContext

Information about application:
Class Name: org.apache.catalina.core.ApplicationContextFacade
Major Version: 4
Minor Version: 0
Server Info: Apache Tomcat/9.0.12
Serlet Context Name: Welcome to Tomcat

A new session will be established, if this JSP page is requested for the first time. Subsequent requests will share the same session. Click the refresh button on the Web browser, you will see that the session ID will be the same, the create time will be the same, but the last access time will be the new time, and "is session new" will be "false".

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 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

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB