|
Comments
|
Today's Top SOA Links
BF on CF Java for CFers - Part 2 of 3
Java for CFers - Part 2 of 3
By: Ben Forta
Nov. 27, 2000 12:00 AM
As I explained last month (and in several columns earlier this year), Java is here to stay, and Allaire is fully embracing the technology. For ColdFusion developers this is a scary proposition. The thing we love most about CF is that it's easy, simple, and rapid - and those arent adjectives usually used to describe Java. Or, rather, it was a scary proposition. As I showed you last month, server-side Java can actually be relatively painless. The trick is to use JSP - JavaServer Pages, a scripting-style interface that gives you access to all the benefits of Java without the pain. As promised, this month we'll continue our look at JSP (using Allaires JRun application server, which I strongly recommend that you download and play with).
URL Parameters and Form Fields
<A HREF="page2.cfm?fname=Ben&lname=Forta">Click</A> In ColdFusion you could display the passed data as simply as this:
<CFOUTPUT> The JSP equivalent is just as simple. Within JSP pages a set of objects are always available to you. Now before you panic, don't let the term object scare you - you don't need to understand object-oriented development to use these objects, as you're about to see. There is a whole range of objects you can use to perform all sorts of operations. For example, the "application" object is used to read and write application-wide data (much like ColdFusion APPLICATION variables), the "session" object is used to work with session variables (much like ColdFusion SESSION variables), and the "response" object is used to send data (for example, cookies) back to the browser (much like using the <CFCOOKIE> tag, for example). One of the most important objects is the "request" object, which is used to retrieve information that is passed from the browser during a request. This is the object youd use to return URL parameters (and form fields too). The following is the JSP equivalent of the above CFML code:
Hello <%=request.getParameter("fname")%> JSP objects have "methods" associated with them - which can be thought of as functions pertaining to an object. Any interaction with an object occurs via the appropriate methods; "request" has a method named getParameter, which is used to retrieve a specified parameters value. request.getParameter("fname"), for example, retrieves the fname parameter. Of course, if you prefer, you could retrieve the values and save them to variables for later use (as you could do in ColdFusion). Heres a commented example of this:
<%--- Get URL parameters ---%> JSP doesn't distinguish between parameter types, so if fname and lname had been FORM fields, the code would have still worked as is. And unlike ColdFusion, JSP wont throw an error if the specified parameter doesn't exist. Instead it'll return an empty string. As you can see, access user data is only slightly more involved than it is in ColdFusion.
Managing Sessions
Heres how youd set a SESSION variable in CF: <CFSET SESSION.name="Ben"> And here's the JSP equivalent: <% session.setAttribute("name", "Ben") %> Reading session variables is just as easy. Heres the CFML syntax: SESSION.name And here's the JSP equivalent: session.getAttribute("name"); Simple as that. Oh, and you'll like this one - you don't have to lock variable access in JSP.
Working with Databases
There is no JSP tag or language element for database access. We ColdFusion developers take tags like <CFQUERY> (and <CFMAIL>, <CFPOP>, etc.) for granted sometimes. Developers using other languages often find themselves jumping through hoops to achieve the same functionality. And as database access is such an important part of application development, this warrants special attention. So, to set the stage, here's some simple CFML code, the kind you and I write every day:
<!--- Retrieve data ---> Now you're really going to appreciate CFML. As JSP has no special database support, developers have to use lots of embedded Java. The equivalent of the above code (take a deep breath and make sure you're sitting down) could look a lot like this:
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,java.io.*"%> Okay, so calling it a little ugly was a bit of an understatement - truth is, it's downright horrid. In fact, it's not even JSP, it's Java, plain and simple. JSP is a simplified interface to Java, but when the appropriate simplification doesn't exist, JSP developers must resort to using Java. Or must they? Note: Like ColdFusion developers, JSP developers use database drivers to interact with databases. But instead of ODBC, they use a JDBC driver (similar to JSP, but Java based). ODBC drivers actually can be used with JSP (via a special piece of software called a bridge), but for performance and scalability reasons you won't want to use ODBC drivers this way on a production site.
Once Again, It's All About Tags
Which is great, except that standardized Tag Libraries for the most part don't exist yet - youll have to create your own. Unless, of course, you happen to be using JRun as your JSP server (why on earth would you use any other product anyway?). JRun comes with a set of Tag Libraries, which (and this should come as no surprise) include all sorts of tags that closely mimic the behavior of CFML tags. You can use the sql tag (which is similar to <CFQUERY>) and the foreach tag (which is similar to <CFOUTPUT>) and pass the appropriate attributes to them - the tags do all the heavy lifting. The following is a block of JSP code (using JRun Tag Libraries) that accomplishes the same thing as the previous Java code:
<%@ page import="allaire.taglib.*,java.sql.*" %> Okay, so it isn't CFML, but it is orders of magnitude better than straight Java. Once again (as we CFers knew year's ago), the future of application development is tags.
Summary
Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||