Section 13.1. Using Java Technology to Create Web Sites

   

13.1 Using Java Technology to Create Web Sites

The ability to execute Java code from within ColdFusion is not new to ColdFusion MX. Previously, this functionality was available to developers using the <cfobject> or <cfapplet> tags, and via CFX tags. Use of these technologies to bring Java functionality into ColdFusion has been accepted and popular for years . However, there are times when the requirements of your project demand that a Java Web technology be used. Perhaps your client has existing systems written in Java or prefers to use open source, freely available technology. In these cases, using Java technology to create your dynamic sites may be the best solution.

Java provides a number of ways that you can create dynamic functionality within Web pages. JavaServer Pages (JSP) and servlets are increasingly popular ways of doing this. JSP and servlets are capable of performing everything that ColdFusion is capable of and more. Because the entire Java programming language is available to the developer, very sophisticated Web applications can be built using these technologies. JSP and servlets allow the developer to interact with HTML, relational databases, JavaBeans, and Java code to create dynamic Web pages. Here we will overview servlets and JSP in turn .

13.1.1 Overview of Servlets

Servlets, in version 2.3 as of this writing, were introduced by Sun to extend the power of the Web server. Much of the initial excitement surrounding Java was due to the fact that small applications (applets) could be downloaded and executed securely on the client. (Note that there is no theoretical limit on the size of the program executable as an applet). Applets were secure because they were executed inside the Java Virtual Machine and had no potentially dangerous access to the underlying operating system. The popularity of applets began to suffer, however, as they eventually succumbed to the browser wars between Microsoft and Netscape. If you have experienced difficulty in ColdFusion 5 using CFGRID , CFTREE , or CFSLIDER , you can understand how applets began to reveal certain limitations. The applet plugins for different browsers were incompatible, which undermined one of the fundamental propositions of Java ”that programs are platform-independent. Moreover, the trend in Web development was decidedly toward server-side functionality. ColdFusion, Perl, and ASP had been working in this arena for years with great success. So Sun introduced the servlet .

An applet is a small application. Servlets, then, act as small servers. A servlet is a Java program that is executed on the server when the Web server receives a request mapped to that servlet. Like applets, servlets are essentially regular Java classes. The primary differences are that servlets require a Web server environment in which to run, and they extend javax.servlet and javax.servlet.http . The javax.servlet package contains all of the classes and interfaces that are used by servlets. The javax.servlet.http package contains all of the interfaces required to create HTTP-specific servlets.

Servlets are part of the Java 2 Enterprise Edition and must be downloaded separately to be used with the Standard Edition.

13.1.2 Overview of JSP

JavaServer Pages, in version 1.2 as of this writing, were introduced by Sun in 1999. JSP is a tag-based language with an XML syntax. There are nine JSP tags, as compared with 85 in ColdFusion. This attests nothing, however, to the scope of the language or how long it might take to become comfortable with JSP. This is for a variety of reasons. First, JSP serves as a high-level abstraction of servlets. Writing a JSP is somewhat like writing a ColdFusion template, in that you write tags that call certain functional units and return a result or forward to another page. The JSP engine is a servlet that is mapped to the *.jsp extension. When a .jsp page is called in a browser, that page is translated into a servlet, which is saved into a temporary directory; at that point the servlet is processed .

There is little one can do using the few standard JSP tags alone. In order to leverage the full power of JSP, one generally writes JavaBeans and implements custom tags (which in JSP are called Custom Actions). A JavaBean is rather like a ColdFusion Component. It is a regular Java class that defines getter and setter methods ”for instance, setPrice() and getPrice() ”for the object in its domain. The Bean is then invoked from the JSP to do the heavy lifting . The purpose of this kind of architecture is to maintain a strict separation between a Web site's logic and its presentation layers .

The remainder of this chapter will be devoted to comparing these technologies with those of ColdFusion, installing and configuring a servlet container, and introducing standard JSP.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net