ColdFusion is built on a Java Enterprise Edition (JEE, formerly termed "J2EE") technology platform. This structure lets ColdFusion applications take advantage of, and integrate with, JEE elements. ColdFusion pages can do any of the following:
- Use JSP tags.
- Interoperate with JSP pages.
- Use Java servlets.
- Use Java objects, including JavaBeans and Enterprise JavaBeans.
The cfapplet tag simplifies using Java client-side applets.
Use an applet on a ColdFusion page
- Register the applet .class file in ColdFusion Administrator Java Applets Extensions page. (For information on registering applets, see the ColdFusion Administrator online Help.)
Use the cfapplet tag to call the applet. The appletSource attribute must be the Applet name assigned in the ColdFusion Administrator.
For example, ColdFusion includes a Copytext sample applet that copies text from one text box to another. The ColdFusion Setup automatically registers the applet in the Administrator. To use this applet, incorporate it on your page. For example:
<cfapplet appletsource = "copytext" name = "copytext">
About ColdFusion and JSP
ColdFusion supports JSP tags and pages in the following ways:
- Interoperates with JSP pages: ColdFusion pages can include or forward to JSP pages, JSP pages can include or forward to ColdFusion pages, and both types of pages can share data in persistent scopes.
- Imports and uses JSP tag libraries: the cfimport tag imports JSP tag libraries and lets you use its tags.
ColdFusion pages are not JSP pages, however, and you cannot use most JSP syntax on ColdFusion pages. In particular, you cannot use the following features on ColdFusion pages:
Include, Taglib, and Page directives: Instead, you use CFML import tag to import tag libraries, and the include (or forward) method of the page context object returned by the ColdFusion GetPageContext function to include pages. For more information, see Using JSP tags and tag libraries and Interoperating with JSP pages and servlets.
Expression, Declaration, and Scriptlet JSP scripting elements: Instead, you use CFML elements and expressions.
JSP comments: Instead, you use CFML comments. (ColdFusion ignores JSP comments and passes them to the browser.)
Standard JSP tags: Such as jsp:plugin, unless your JEE server provides access to these tags in a JAR file. Instead, you use ColdFusion tags and the PageContext object.
About ColdFusion and servlets
Some Java servlets are not exposed as JSP pages; instead they are Java programs. You can incorporate JSP servlets in your ColdFusion application. For example, your enterprise could have an existing servlet that performs some business logic. To use a servlet, the ColdFusion page specifies the servlet by using the ColdFusion GetPageContext function.
When you access the servlet with the GetPageContext function, the ColdFusion page shares the Request, Application, and Session scopes with the servlet, so you can use these scopes for shared data.
ColdFusion pages can also access servlets by using the cfservlet tag, use the servlet URL in a form tag, or access an SHTML page that uses a servlet tag.
Note: The cfservlet tag, which provides access to servlets on JRun servers, is deprecated since ColdFusion MX.
About ColdFusion and Java objects
Java objects include the following:
- Standard Java classes and methods that make up the JEE API
- Custom-written Java objects, including the following:
- Custom classes, including JavaBeans
- Enterprise JavaBeans
ColdFusion pages use the cfobject tag to access Java objects.
ColdFusion searches for the objects in the following order:
- The ColdFusion Java Dynamic Class Load directories:
- Java archive (.jar) files in web_root/WEB-INF/lib
- Class (.class) files in web_root/WEB-INF/classes
ColdFusion reloads classes from these directories, as described in the next section, "About class loading."
- The classpath specified on the JVM and Java Settings page in the ColdFusion Administrator.
- The default JVM classpath.
About class loading
ColdFusion dynamically loads classes that are either .class files in the web_root/WEB-INF/classes directory or in JAR files in the web_root/WEB-INF/lib directory. ColdFusion checks the timestamp on the file when it creates an object that is defined in either directory, even when the class is already in memory. If the file that contains the class is newer than the class in memory, ColdFusion loads the class from that directory. To use this feature, make sure that the Java implementation classes that you modify are not in the general JVM classpath.To disable automatic class loading of your classes, place the classes in the JVM classpath. Classes located on the JVM classpath are loaded once per server lifetime. To reload these classes, stop and restart ColdFusion.
About GetPageContext and the PageContext object
Because ColdFusion pages are JEE servlet pages, all ColdFusion pages have an underlying Java PageContext object. CFML includes the GetPageContext function that you can then use in your ColdFusion page. The PageContext object exposes several fields and methods that can be useful in JEE integration. In particular, it includes the include and forward methods that provide the equivalent of the corresponding standard JSP tags. For more information on other features of the PageContext object, see Java documentation. You can find the Javadoc description of this class at http://docs.oracle.com/javaee/7/api/javax/servlet/jsp/PageContext.html.
About CFML variables and Java variables
Because ColdFusion variables are case-independent and Java variables are case-dependent, be careful about variable names. Use the following rules and guidelines when sharing data between ColdFusion and Java code, including JSP pages and servlets.
- If you use mixed case variables, all variable names must be unique, independent of case. For example, you must not have two Java variables, MyVariable and MYVARIABLE. ColdFusion cannot distinguish between the two.
- If you share Request scope variables between a CFML page and a JSP page or servlet, all shared Request scope variable names must be all-lowercase in the JSP page or servlet. Mixed case or all-uppercase variables cause null pointer exceptions if CFML refers to these variables.
- If you share Application or Session scope variables between a CFML page and a JSP page or servlet and use a named ColdFusion application (the common usage), the variables on the JSP page or servlet are case-independent.
- If you share the Application or Session scope variables between a CFML page and a JSP page or servlet, and use an unnamed ColdFusion application, the variable names in the JSP page or servlet must be all lowercase.
- When you specify a class name in the cfobject tag or CreateObject function, the name must be case-correct.
- You can prevent problems by consistently using all-lowercase variable names.
- In your CFML, use the same case as you do in your Java or JSP. Doing so does not change how the application works, but does help prevent confusion.