LAMP Vs. J2EE: A Tale Of Two Platforms

In the first of two articles, Ross Greenberg explains what makes LAMP a bright idea for certain types of development jobs.

InformationWeek Staff, Contributor

February 8, 2005

5 Min Read

Put two Linux geeks in a room, and you'll get three opinions. Better yet, ask a question like "which distro is best?" or "which platform is best?" in the alt.linux. newsgroup, step back, and watch the fireworks. In fact, Linux-heads will agree on exactly one thing: There's nothing worse than Windows! On any other point, these debates can get bloody.

In this two-part series, we'll explore the development platforms launched a thousand debates: the open-source software stack combining Linux, Apache, MySQL, and Perl (or PHP or Python), known as LAMP; and Sun Microsystems' Java 2, Enterprise Edition (J2EE) platform. Today, I'll discuss the LAMP software stack, and we'll look at an example of how to use LAMP to build and serve dynamic Web pages.

You Just Had To Ask
This type of article raises an obvious question: Which platform is better?

First, to save time, you'll find my email address at the end of this article, already soaked down with flame retardant.

Now, here's my answer: They're both worthy platforms. And they both have some problems, too.

Can We All Get Along?
Depending on whom you ask, LAMP is Sun's response to J2EE, or J2EE is Sun's answer to LAMP. Sun's official position is that LAMP is suitable mostly for lower-end applications, while J2EE is directed towards, well, enterprises. Sun does not consider the two platforms to be competitors, and it supports both of them.

The LAMP stack relies on interpretive code, which gets compiled and executed on an as-needed basis every time it runs. Even if a process runs the same code multiple times, it will repeat the same set of processes each time, eating a lot of CPU cycles. On the other hand, code that won't hit the interpreter doesn't even need to have its syntax checked (this also happens at runtime).

J2EE code, by contrast, is compiled: If you change one line of code, you'll have to recompile the entire module containing the change, and possibly others as well. In most cases, however, compiled code executes faster than interpretive code on any given hardware platform or operating system.

Get On The Scale
Many developers assume LAMP doesn't scale very well. My own experience shows that building a prototype LAMP application and hoping it will scale is dangerous thinking. I eventually discovered, however, that the LAMP scalability problems I encountered were actually the result of a poorly designed MySQL database table. A database redesign cleared the bottleneck and solved the scalability problem--or at least my scalability problem.

Interpretive code also, in many cases, enables faster development cycles and makes it easier to tweak the finished product. Changing the appearance of dynamic Web pages, for example--a common job for the LAMP platform--can be a surprisingly painless process. Doing the same job with compiled code, including the monolithic, compiled object code that's typical of J2EE applications, is usually more tedious and time consuming. Developers sometimes complain that J2EE is over-engineered and unnecessarily complex, and it certainly requires more sophisticated, careful planning and design. Down-and-dirty coding is not a smart approach for any developer to take towards a J2EE job!

Light Your LAMP
Assuming you already have a Linux server up and running, you'll need to install the Apache Web server, as well as MySQL and perhaps PHP. (The "P" in LAMP can also mean Perl or Python, two other interpretive, open-source languages.) As I mentioned above, one of the most powerful aspects of a LAMP application is its ability to "Webify" content quickly and easily, combining the data stored in a MySQL table with some relatively simple PHP or Python code. Using PHP version 5.0, for example, a developer can direct a simple "print" statement to generate output that is then parsed, converted to an HTML file, and served on a Web site courtesy of Apache.

Dynamic Web sites implement PHP code in a variety of ways, but the most popular method involves surrounding commands with a <?php/?> combination. PHP is a new language for many Linux users, but it's easy to pick up the basics. Let's use a simple HTML file, for example: <html> <body> <?php print "<H1>Tester1<H2>Tester2"; ?> </body> <html> Now we'll add MySQL to the mix. Log onto the server, run mysql, and enter:

SHOW tables;
CREATE TABLE emp (name VARCHAR(50), id VARCHAR(5));
SHOW tables;
INSERT INTO emp VALUES('John Smith', '00001');
INSERT INTO emp VALUES('Jane Doe', '00002'); 

This creates a simple database called "test" with a single table called "emp". The emp table has two fields: a 50 character "name" field and a five character "id" field. We'll insert two members and then quit.

Now to tie that into simple PHP code with this snippet (install it in a file called "pipeline.php," for example): <?php $link = mysql_connect("localhost", "root", "") or die("Connect failed: " . mysql_error()); print "Good connect"; mysql_select_db("test") or die("Could not select test database"); $query = "SELECT * FROM emp"; $result = mysql_query($query) or die("Bad Query?" . mysql_error()); print "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_val) { print "\t\t<td>$col_val</td>\n";} print "\t</tr>\n"; } print "</table>\n"; mysql_free_result($result); mysql_close($link); ?> This snippet uses PHP to fetch the fields from the emp database table and, for each database member, to display the members (names and their ids) in a table. Look at the file in your favorite browser and revel in your new Web development powers.

This was a simple example, but it shows both the power and the simplicity LAMP offers. Feel free to edit the PHP file above and play around: It's easy to experiment with interpretive code. Try to make the <Table> prettier, embolden stuff, give it a page background. In other words--go for it!

Stay tuned: Next time, we'll take a closer look at J2EE.

Ross M. Greenberg has been coding in and under Unix before there even was a Linux. You know, back when the big religious wars were aboutt C versus C++ and whether "bang-stop" notation is better than "at-sign" notation. (He was on the losing side.)

Never Miss a Beat: Get a snapshot of the issues affecting the IT industry straight to your inbox.

You May Also Like

More Insights