The Web is now the best way to deploy your applications. It is because of the ease of use and reach to your users and, of course, the fact that you only have to deploy one version of your code for everyone to use.
To get this done, you would have probably looked at different languages and even frameworks out there. Did they seem complicated to get going? Were there lots of new terms to learn?
This is where Railo Server comes in. It provides an easy way to write and deploy your applications using a language that is very similar to HTML (http://en.wikipedia.org/wiki/HTML), about which you'd already know if you happened to work with static websites.
If you are already using HTML, Railo Server is a perfect addition to your toolkit!
In this book, you will learn how to use Railo Server to develop web applications in a very efficient manner. We will also introduce the different features and options available to get things done easily and quickly as we go along.
In this chapter, we will:
Introduce you to the Railo Server
Introduce you to some of the features of Railo Server
Show you why Railo Server and CFML make developing web applications a breeze
Let's dive right in!
HTML per-se (without JavaScript) is a static language, which means that you cannot query databases, send e-mails, execute searches, or generally interact with the server, and return dynamic results to the user.
Imagine that you need to do one of the above tasks. HTML doesn't offer any solutions for these kinds of problems because it is just a way to display information and not manipulate other systems.
What can we do in order to overcome this problem?
ColdFusion Markup Language (CFML) neatly fits into the HTML syntax and allows you to place CFML tags in-between HTML tags. Basically, Railo Server generates HTML, which is then interpreted by the browser.
Depending on the tags that you are using, different HTML pages are sent back to the client.
Let's have a look at an example Railo Server template:
<html>
<head><title>An Example Template</title></head>
<body>
<div id="AccountHeader">
<cfif SESSION.loggedIn>
<h3>Private bank account No.:
<cfoutput>#bank.accountNr#</cfoutput>
Show secret information
</cfif>
</div>
</body>
</html>
The highlighted code above shows you some CFML (or Railo Template) code. This code is dynamic and will run on the server before returning the rendered HTML back to the browser that requested the page.
If you are used to reading HTML code, you can easily figure out what the previous code is supposed to do. (It displays the HTML between the <cfif>
tags if the variable loggedIn
in the SESSION
is true.)
HTML per-se (without JavaScript) is a static language, which means that you cannot query databases, send e-mails, execute searches, or generally interact with the server, and return dynamic results to the user.
Imagine that you need to do one of the above tasks. HTML doesn't offer any solutions for these kinds of problems because it is just a way to display information and not manipulate other systems.
What can we do in order to overcome this problem?
ColdFusion Markup Language (CFML) neatly fits into the HTML syntax and allows you to place CFML tags in-between HTML tags. Basically, Railo Server generates HTML, which is then interpreted by the browser.
Depending on the tags that you are using, different HTML pages are sent back to the client.
Let's have a look at an example Railo Server template:
<html>
<head><title>An Example Template</title></head>
<body>
<div id="AccountHeader">
<cfif SESSION.loggedIn>
<h3>Private bank account No.:
<cfoutput>#bank.accountNr#</cfoutput>
Show secret information
</cfif>
</div>
</body>
</html>
The highlighted code above shows you some CFML (or Railo Template) code. This code is dynamic and will run on the server before returning the rendered HTML back to the browser that requested the page.
If you are used to reading HTML code, you can easily figure out what the previous code is supposed to do. (It displays the HTML between the <cfif>
tags if the variable loggedIn
in the SESSION
is true.)
Railo Server is a service that can be installed on any Java Application Server (http://en.wikipedia.org/wiki/Java_Servlet) that helps you write web applications quickly and easily, without the complexities normally associated with developing fast, scalable, and secure applications in Java.
Railo Server is a servlet that runs on any servlet engine. Servlets are small (or large) programs that are invoked by a servlet engine or a J2EE application server (such as Tomcat, JRun, Jetty, Resin, Glassfish, IBM WebSphere, BEA WebLogic, and others).
The application servers run within the Java Runtime Environment (JRE) and call the corresponding servlet; if a certain request comes to it, it matches certain conditions.
If this is the first time you are hearing about servlets, JREs, and similar things, no need to worry, Railo Server can be installed with an easy installer for a complete setup. It installs the JRE, the servlet engine, and can even hook into an existing IIS or Apache web server. What the servlet does is completely open. In this case, the Railo Servlet allows us to do the following:
Compile CFM files into Java bytecode
Check the syntax of invoked files
Invoke the necessary files according to the CFML syntax
Execute the bytecode and throw any errors that may occur
Build up a request environment
Interact with external resources, including:
Databases
Cache systems
Filesystems (virtual or physical)
Web services
Event Gateways
Write files
Return HTML, JSON, XML, PDF, or anything else that an HTTP request can generate and handle
Execute scheduled tasks
Send e-mails
Create and read RSS and ATOM feeds
Manage sessions
If you need to build a dynamic web application, Railo Server comes into play. You can use Railo Server in order to program your applications in CFML and run your application either in an externally-hosted server or a local server.
Basically, when a client enters a URL into his/her browser address line a request is made to a Railo Server. The process is the following:
1. The URL is resolved to an IP address by checking the DNS system.
2. The browser sends the request to the server at the retrieved IP address.
3. The web server on the target server is invoked (since that runs on port 80).
4. The web server checks what kind of extension or filter definition matches the URL (
.cfm
or.cfc
).5. The application server is invoked (assuming it is responsible for the extension
.cfm)
.6. The application server calls the corresponding servlet (in our case, Railo).
7. Railo processes the request.
8. Railo sends back HTML (or whatever else) to the application server.
9. The application server sends the response back to the web server.
10. The webserver sends the HTML response back to the client.
11. The client browser interprets the returned HTML.
So, basically with Railo, you will dynamically create static pages. The following image illustrates this process:
In essence, Railo Server provides the web developer with CFML, which is a simple yet powerful language, as well as a highly configurable server. This helps you write applications extremely quickly.
Railo itself is written in Java, and therefore the libraries deployed with Railo are JAR files.
Your CFML code will be compiled to Java, so you could say that programming in Railo CFML is ultimately programming in Java because Railo generates Java and disguises the complexity of Java applications from the programmer.
Whenever you use a programming language, the features and tools that come with the language are most influential in helping you decide whether you made the right choice for the current project.
Railo Server's strengths are geared towards Rapid Application Development (RAD). Once you use Railo Server, you will find that you can create websites literally within hours. For example, you can build a blog within an hour or two.
Time is a huge asset and any tool or any programming language that saves you time is something you should take a closer look at.
Let's take a look at how easy it is to send an e-mail with Railo in comparison to other programming languages:
First of all, this is how you would send an e-mail in PHP:
<?php Railo Serveremail, sending in PHPrequire_once "Mail.php"; $from = "Sandra Sender <sender@example.com>"; $to = "Ramona Recipient <recipient@example.com>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "mail.example.com"; $username = "smtp_username"; $password = "smtp_password"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?>
Let's have a look at the same functionality in Java:
import javax.mail.*; Railo Serverin Javaimport javax.mail.internet.*; import java.util.*; public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException { boolean debug = false; //Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", "smtp.jcom.net"); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Optional : You can also set your custom headers in the Email if you Want msg.addHeader("MyHeaderName", "myHeaderValue"); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); }
And finally, let's compare that to how we would do it in CFML:
<cfmail from="Sandra Sender <sender@example.com>" Railo Serverin CFMLto="Ramona Recipient <recipient@example.com>" subject="Hi!"> Hi, How are you? </cfmail>
As you see, this is a lot of functionality with very little code!
Another comparative example would be the number of lines of code required to create a web service. This shows you how Railo Server really can speed up your development:
PHP:25 lines
Java:16 lines
Railo:1 line
In terms of productivity, Railo Server is very powerful and can match any other programming language in performance and especially in conciseness, as we will discover with many of the examples in this book.
In addition, Railo Server lets you manage your resources such as databases, mail servers, and even the behavior of the server itself with the included Railo Administration application.
Another feature that the Railo Server includes is session management, allowing the state of users to be persisted across several requests.
This feature is also expanded in being able to store general code settings in an application scope. This is kept in memory so that the environment doesn't have to be built up on each request; this, means great performance benefits right out of the box for your application.
With Railo Server, scaling and clustering is extremely easy to achieve without having to configure a full-blown J2EE cluster.
Starting with basic CFML, we can explore the full power of Railo components, caching features, database interaction, and last but not least, Java interaction.
This section will outline some of the features that also come with Railo Server and will give you some idea of the power behind it.
Railo Server has an integrated bytecode generator (written with ASM) that directly compiles CFML written templates into Java bytecode. The compiler is very fast and, on average, it compiles a template file in less than 10 milliseconds.
In early versions, Railo Server translated CFML code into XML, then used XSL to convert it into Java, and finally used a Java compiler to generate bytecode. Since version 2.0, Railo has had an integrated bytecode generator. The compiler is able to spot syntax errors in templates and throw errors accordingly.
Railo Server is capable of using Railo archives that are compiled versions of your templates. This allows you to package and distribute your applications without having to worry about other people viewing your code and possibly stealing your intellectual property. In addition, the size of your application decreases drastically and it executes a lot faster (as it is already compiled).
Railo Server has more than 125 tags and over 500 functions that allow you to create the best applications in no time. There are tags and functions for nearly every task you can think of, from manipulating arrays to converting images. The great thing is that Railo Server is also extendable, so you can create your own functions and tags to extend your application or the server itself.
CFML components give you the power you need in order to scale and design MVC-based applications. They allow you to use Object Oriented Programming (OOP) techniques such as methods, encapsulation, and inheritance. This leads to robust, encapsulated code, and, in the long run, reduced maintenance costs.
Railo Server also integrates a scripting version of the tag-based programming language. Programmers who are familiar with the coding syntax of other languages, such as JavaScript, PHP, or JSP, will enjoy this feature.
For example, you can create a component using tags as follows:
<cfcomponent output="false"> <cffunction name="init"> <cfreturn this /> </cffunction> </cfcomponent>
You can also use the cfscript
format to achieve the same thing:
component output=false{ function init(){ return this; } }
With the web and server administrator, Railo Server offers a very easy tool in order to configure the behavior of local and global applications. The Web and Server Administrator applications are the main tools you will use in order to interact with the behavior of Railo. Of course, these applications are also built using CFML. So, you can programmatically adjust all the settings from CFML itself.
Railo Server integrates a task manager that allows you to asynchronously execute requests in the background.
Railo Server tries to include everything you need, but sometimes there are things that are very specific to your application. For this, there is an Extension Manager that allows you to add features to Railo Server directly from the Railo Server Administrator application. The extension store offers programmers a whole new set of features and applications that are easily installed and updated.
Extensions are not the only thing that can be easily updated. You can update Railo Server itself to the latest version with just a click. In the Railo Server administrator, you will get notifications as soon as a new release of Railo Server is available and this allows for a one-click update. If you need to restore the old version again, it is also just one click away. Normally, it is only a matter of seconds.
When developing Railo Server, it was a strict goal to keep compatibility with the CF standard as tightly as possible. This is demonstrated by the fact that with various applications, a change of the application server to Railo did not change anything in the runtime behavior of the application itself, except maybe for the improved speed. So, if you already have some CF applications running, there's no reason to fear high migration costs.
At the moment, all of the major CFML frameworks or Content Management Systems (CMS) work with Railo Server. So, if you are used to using a framework or tool like FW/1, ColdSpring, ModelGlue, CFWheels, or ColdBox, you don't have to fear incompatibilities. In fact, FW/1 is even written by one of the members of the Railo team, Sean Corfield.
With Railo Server, global security settings can be made for all applications running on the server. For example, access to the filesystem can be denied for a single application or restricted only to files that lie within the web root of the application.
In Railo Server, it is very easy to interact with different virtual filesystems (VFS). The local hard disk is just an instance of a virtual filesystem. Other VFSs that Railo supports are RAM, HTTP, DB, FTP, SFTP, ZIP, TAR, S3, and others.
Railo Server's main goal was to be the CFML engine with the best performance. One of the main reasons why this goal can be achieved is because Railo uses common resources for all applications. Additional changes in the architecture and various internal structures allowed us to push the performance to higher limits. To the end user, these changes are noticeable in a short response time, during the execution of the same code on the various engines.
The easiest way to give Railo Server a try is to download Railo Express, unpack it, and hit a batch file. There is no easier way to install the software. Railo Server can also be downloaded as an integrated installer for various operating systems and will install Apache Tomcat and add connectors from web servers, such as Microsoft's IIS and the Apache HTTP server.
Railo Server is free and an open source LGPL V2 allowing you to both use and re-distribute your applications with the underlying engine.
When it comes to using Railo Server in a clustered environment, there are some useful and inexpensive extensions that can be purchased. Just use the Railo Extension Manager that is part of the Railo Administration Application in order to see what extensions are available.
Because Railo Server is free, you don't have to fear any update cost or high-initial cost if you plan to use it in a large environment.
Hopefully, this chapter has given you an overview of what Railo Server offers the web developer in terms of ease of programming, conciseness of language, and feature set.
You should now have an idea of:
The small number of lines you need to write to get things done
The rich number of features that Railo Server provides
How easy it is to extend Railo Server by using extensions
The way templates are processed and delivered to the client
The powerful Java underpinnings that are made available to you without any complexity
In the next chapter, we shall have a look at the various ways you can install Railo Server and how to get up and running quickly under different environments.