29

May

Your CFML Engine is a Java Servlet
 

You may have heard in the past that Lucee, like the other popular CFML engines, is a Java servlet — or more precisely, a JSP servlet.  But what does that mean exactly? And more importantly, why should you care?

Let’s start by clarifying some terms:

  • Java Servlet Specification - the Servlet API, as defined by JSR-369 [1], is a blueprint for a Java based application server, which:

    • Accepts requests from clients
    • Processes them
    • And usually returns a response
  • Servlet Container - the Controller component of the Servlet specification.  This is the boilerplate code that parses the config files and initializes the servlets, and then accepts requests from clients and passes them to servlets and filters for processing.  Some popular servlet containers include Apache Tomcat [2] and Eclipse Jetty [3].

  • Java Servlet - the code that runs inside the Servlet Container and does the actual processing, i.e. implements the business logic. The fundamental part of the Servlet interface [4] is the service(ServletRequest, ServletResponse) method, which is called by the Servlet Container for each request that requires processing.   

  • JavaServer Pages (or JSP) - the JSP specification [5] builds on top of the Servlet API, and defines an API [6] that provides an infrastructure for creating HTML templates that can be populated when executed.  The concept is similar to CFML, though some (including yours truly) might argue that the JSP code is more cryptic and takes longer to write.   

So Lucee is a Servlet - if you look at the hierarchy of Lucee’s CFMLServlet [6], you will see that it extends javax.servlet.http.HttpServlet, which in turn implements the javax.servlet.Servlet interface. 

In fact, Lucee is a JSP Servlet - its CFMLFactory [7] extends javax.servlet.jsp.JspFactory, and it produces PageContextImpl objects, which are an implementation of the abstract class javax.servlet.jsp.PageContext [8] from the JSP specification.

You can verify that programmatically by calling the following function on the PageContext object:

/**
* returns an array with the class hierarchy of the object
*/
function getClassHierarchy(object){

    var hierarchy = [];
    var class = object.getClass();

    while (!isNull(class)){
        hierarchy.append(class.getName());
        class = class.getSuperClass();
    }

    return hierarchy;
}

If you run this function on Lucee [9], you will see the following hierarchy: 

java.lang.Object
 javax.servlet.jsp.JspContext
   javax.servlet.jsp.PageContext
     lucee.runtime.PageContext
       lucee.runtime.PageContextImpl

On Adobe ColdFusion 2018 [10] you will see: 

java.lang.Object
 javax.servlet.jsp.JspContext
   javax.servlet.jsp.PageContext
    coldfusion.runtime.NeoPageContext

What does it all mean, and why should you care?

There are many benefits to working with products that adhere to standards and specifications, but the most important ones are documentation, consistency, and the freedom to switch vendors.

Since the Java specification is defined before the first line of code is written, it has to be detailed and clear.  It might not be the most interesting read in your library, but if you are looking for information about Servlets, or JSP, you will most likely find it in the specification.

Consistency comes from the fact that vendors have to adhere to the specification.  If the specification declares that under condition A, you can expect b, c, and d, then you can expect that to be true for any implementation that follows the specification.

The freedom to switch vendors is my favorite.  If the system that you are running has new updates that you do not like, or has not been updated in a long time, or another system has added compelling new features, you have options.  Did you find that the most recent version of Tomcat runs on your system better than Jetty? you can switch with confidence, knowing that the systems will adhere to the specifications and execute your code consistently!

Ever tried to find documentation about the inner workings of your CFML engine?  Or perhaps you tried to use some 3rd party code library and couldn’t find good tutorials or references, or tried to hack some advanced Java integration?  You’re in luck! There is a wealth of information out there about Servlet and JSP technologies, and many times when you are looking for an advanced solution, you might find the information that you are looking for there.  

 

[1] http://download.oracle.com/otndocs/jcp/servlet-4-pr-spec/index.html

[2] https://tomcat.apache.org/

[3] https://www.eclipse.org/jetty/

[4] https://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html

[5] http://download.oracle.com/otn-pub/jcp/jsp-2_3-mrel2-eval-spec/JSP2.3MR.pdf

[6] https://docs.oracle.com/javaee/7/api/javax/servlet/jsp/package-summary.html\

[7] https://github.com/lucee/Lucee/blob/master/loader/src/main/java/lucee/runtime/CFMLFactory.java

[8] https://docs.oracle.com/javaee/7/api/javax/servlet/jsp/PageContext.html

[9] https://trycf.com/gist/3537521a10aee44677eae1de18237365/lucee5

[10] https://trycf.com/gist/83d3c269d6e8d0132b099a8459b1a841/acf2018


Social Media

FOLLOW US