With ColdFusion MX 7, each of your CFML pages is being converted to a Java servlet on-the-fly. It's natural that you should be able to access the various objects and methods exposed by the Java Servlet API defined by Sun. In other words, if you feel like using the same methods and interfaces that Java developers use when writing servlets by hand, you are free to do so. ColdFusion MX 7 uses the GetPageContext() function that gives you access to the underlying page context, which is a servlet term that refers to the current page request. The GetPageContext() function doesn't take any parameters; it just returns the current page context object. The returned object will be a descendant of the abstract javax.servlet.jsp.PageContext class, which just means that the object will have all the methods exposed by PageContext, plus any additional methods supplied by the J2EE server that is actually running ColdFusion. Table 29.5 shows some of the interesting methods exposed by PageContext. A complete listing is beyond the scope of this book; the intention here is mainly to give you an idea of the kinds of methods available to you via the page context metaphor. Complete references for all the items listed in Table 29.5 (and much more) can be found in the Java 2 Enterprise Edition documentation, which was freely available from http://java.sun.com at the time of this writing.
NOTE Page context functionality is probably most useful and interesting to developers who have worked with Java servlets or Java Server Pages (JSP). In general, most of the methods exposed by the page context have direct counterparts in CFML, so you might as well use the direct CFML representations of the functionality exposed by the page context and its members. That said, there may be special situations where the items in Table 29.5 will provide functionality that you can't get from CFML alone; we'll explore some of those situations throughout the remainder of this chapter. The following quick example includes a JSP page. As will be discussed in the next section, the JSP page will be able to access the various CFML variables that may have been set in previous lines of ColdFusion code: <cfset getPageContext().include("myPage.jsp")> Or, to execute certain code only if a secure connection is being used between the browser and the server: <cfif getPageContext().getRequest().isSecure()> ... </cfif> NOTE There are other ways to implement the <cfif> test shown here, such as testing the value of CGI.SERVER_PORT. Using isSecure() might be considered preferable, however, especially since CGI variables tend to vary a bit among Web servers. |