Extending ColdFusion with Servlets
by Edwin Smith
Introduction
Allaire Corporation's Cold Fusion Markup Language (CFML) is perhaps
the easiest way to generate dynamic web content from a
database. Its tag-based scripting commands are simple for HTML authors
to learn, yet powerful enough for building full-featured web
applications. Since 1995, thousands of websites have been developed with
Allaire's popular ColdFusion Application Server. Custom CF tags can be developed either in CFML
or with the CFX plugin API in C++. The market for
third-party custom tags is growing extremely quickly.
In the
meantime, Java servlets are becoming the most effective way to
build server-side web plugins. Servlets can run just about anywhere
and connect to nearly anything. They are faster than CGI scripts
and easier to write than native plugins such as ISAPI, NSAPI,
and CFX. You can write entire applications with servlets or use them as "web
glue" to webify anything from a printer to a mainframe. And Live
Software's JRun is the leading platform for developing and deploying
servlets.
With a few simple examples, this article will demonstrate how to invoke servlets
within your CFML pages using JRun, combining these two technologies into one
great solution. CF_SERVLET
Anyone familiar with the <SERVLET> tag
will immediately understand <CF_SERVLET>. It invokes the
named servlet, running on JRun, and the results are included in the
output of the page. Common HTTP headers, as well as tag attributes,
are available to the servlet using ServletRequest.getParameter(),
just as in NSCA-style Server Side Includes (SSI).
For
example, let's say that we want to invoke Live Software's
EmailLiteServlet (included in Servlet Pack One) to send an email
after a form submission. In SSI, the invocation would look as
follows:
<SERVLET CODE=EmailLiteServlet>
<PARAM NAME="host" VALUE="pop.somehost.com">
<PARAM NAME="dest" VALUE="http://www.myhost.com/successpage.shtml">
<PARAM NAME="from" VALUE="me@myhost.com">
<PARAM NAME="to" VALUE="you@somehost.com">
<PARAM NAME="subject" VALUE="Servlets are cool">
<PARAM NAME="body" VALUE="Because they can run from ColdFusion">
</SERVLET>
The same servlet can be invoked much more simply from ColdFusion as
follows:
<CF_SERVLET CODE=EmailLiteServlet
host="pop.somehost.com"
dest="http://www.myhost.com/successpage.cfm"
from="me@myhost.com"
to="you@somehost.com"
subject="Servlets are cool"
body="Because they can run from ColdFusion">
By default, CF_SERVLET connects to JRun running on the same machine as
ColdFusion. Since JRun's proxy protocol is TCP/IP based, however, you
can invoke servlets anywhere that you have a JRun, even through a
firewall:
<CF_SERVLET CODE=MyCoolServlet
JRUNPROXY=192.168.1.2:8081
Cool="you bet">
And because you are still running inside a ColdFusion page, you can even
dynamically choose where to connect to JRun. For example, the user
could enter it in a form field:
<CF_SERVLET CODE=RenderNewDo
JRUNPROXY=#Form.JRUNPROXY#
Cool=#Form.HAIRSTYLE#>
The
advantages of CF_SERVLET stem from CFML being an easy to use, tag-based scripting
language and servlets being easy to write, powerful
plugins.
How it Works: CFX_JRUN
CF_SERVLET is a CFML wrapper around CFX_JRUN, a C++ based JRun connector. JRun's
out-of-process, connector-based architecture renders it simple to plug into any
server environment (web server or application server). CFX_JRUN hooks JRun into
ColdFusion, translating between the CFX API and JRun's proxy protocol. Like JRun's
other native connectors, CFX_JRUN has a very small footprint. Once CFX_JRUN is
configured into ColdFusion, CF_SERVLET can be dropped into any CFML page, wherever
it is needed.
What about <CF_Anywhere>?
Live Software's <CF_Anywhere> is a servlet-based CFML engine. It translates CFML
pages into Java servlets on the fly, in a manner similar to Java Server Pages
(JSP). Although <CF_Anywhere> also uses the CF_SERVLET tag to invoke servlets,
the implementation is completely different than in ColdFusion. Your custom servlets
can thus be invoked equally well from a CFML page, whether that page is executing
on ColdFusion or <CF_Anywhere>. The same thing cannot be said for other custom
CFX plugins.
Conclusion
ColdFusion, CF_SERVLET, and JRun together make
a great combination: the power of CFML scripting plus the power of
Java servlets.
If you are a CFML developer considering writing a ColdFusion custom tag,
you now have a choice other than CFML and C++. With CF_SERVLET, you can write a
Java servlet that can be used by CFML developers or with any web or application
server.
If you are a third-party Java servlet developer writing web
plugins for your application, a single servlet could replace your
custom ISAPI, NSAPI, and CFX plugins and broaden your customer base
dramatically.
If you are a Java servlet developer who wants to
tap into the huge market for CF custom tags, CF_SERVLET and JRun are
your solution.
Live Software once again provides an
innovative solution by combining JRun, CF_SERVLET, and ColdFusion.
Edwin Smith is a Principal Software Engineer at Allaire Corporation and
works closely with all aspects of JRun engineering, particularly Java/native
integration, system architecture, and performance optimization. Ed is also an
avid motorcycle enthusiast and road racer, winning the amateur lightweight superbike
national title at Daytona in 1996.
|