Accessibility
 
 
Invoking Java Servlets in ColdFusion
By: David Golden, Technical Writer, Allaire, Corp.

Adding the power of Java servlets to your Web site is easier than ever when you combine the strengths of ColdFusion and JRun. However, every journey begins with one small step. To get started on the journey to the wonderful world of Java, Web developers using ColdFusion should become familiar with the two most common methods of invoking servlets: via the URL or using the <CFSERVLET> tag.

Servlets

Java servlets can provide extra services to Web applications by extending server functionality. Servlets process a variety of services more efficiently than other technologies, such as the Common Gateway Interface (CGI) protocol. Servlets can interface with a variety of server-side services, such as legacy application integration, due to Java's platform-independent "write once, run anywhere" architecture. JRun, Allaire's award-winning Java application server, enables most major Web servers and non-Java application servers, including Microsoft IIS, Netscape Server, and ColdFusion Server, to control and execute servlets alongside other applications.

URL Invocation

Much like CGI programs, URL invocation is the most common way to call servlets. The Web server's URL on which the servlet resides, the servlet class name, and the query parameters must be included in each request. For example, here is a sample URL that calls the SimpleServlet example from Sun Microsystems:

http://www.anyhost.com/servlet/simpleservlet

While commonplace, URL invocation suffers from two drawbacks. First, passing parameters via an URL, especially parameters that contain personal information, can never be assured. Second, passing parameters via a URL is not as efficient as using the back door into JRun's proxy protocol — <CFSERVLET>.

<CFSERVLET>

First included in the release of ColdFusion 4.5, <CFSERVLET> quickly became the tag of choice for ColdFusion developers needing the extra functionality of Java servlets. Rather than just passing simple string attributes as with URL invocation, <CFSERVLET> allows complex, typed objects to be passed back and forth.

The <CFSERVLET> tag essentially acts as a surrogate between the Web page and CFX_JRUN, JRun's C++-based connection with ColdFusion Server. When ColdFusion Server executes the <CFSERVLET> tag, it invokes the served named inside the brackets. Here's a simple example using SimpleServlet:

<CF_SERVLET>
	CODE="SimpleServlet"
	WRITEOUTPUT="Yes"
	DEBUG="Yes"
	JRUNPROXY=AnyServer.com:0000
</CF_SERVLET>
Because JRun's proxy protocol is based on TCP/IP, servlets can be invoked on virtually any tier of your server, even behind a firewall. The JRUNPROXY command in the previous code accomplishes this connection.

Conclusion

ColdFusion and Java servlets make a good match for each other. Leveraging the elegant and effective CFML scripting language with the power and extensibility of Java servlets, Web developers will find that their Web applications not only meet their clients' expectations but also exceed them. For additional Java resources, visit the Java page in Allaire's Reference Desk .