Web app Technologies

Many different ways of writing server-side software for web apps have evolved over the years. Early on, the standard was CGI, usually in combination with a scripting language such as Perl. Various web servers also offered native-language APIs for pluggable software modules. Java, howeverand in particular the Java Servlet APIis rapidly becoming the most popular architecture for building web-based apps. Java servlet containers are available for virtually every web server and servlets are becoming foundational for new levels of web infrastructure, such as web services. Why has Java become so popular on the server side? Java is generally faster than scripting languages, especially in a server-side environment where long-running apps can be highly optimized by the VM. Servlets have an additional speed advantage over traditional CGI programs, because servlets execute in a multithreaded way within one instance of a VM. Older CGI apps required the server to start a separate process, pipe data to it, and receive the response as a stream. The unique runtime safety of Java also beats most native APIs in a production web-server environment, where it would be very bad to have an errant transaction bring down the server. Java's portability also means that server-side apps can be scaled up rapidly across different types of hardware without significant porting issues. In many ways, Java has really proven itself in this "business environment" to a greater extent than in the Windows-dominated desktop world. Speed and safety are factors, but perhaps the most important reason for using Java is that it makes writing large and complex apps much more manageable. Java servlets may not be quite as easy to write as scripts, but they are easier to update with new features, and servlets are far better at scaling for complex, high-volume apps. From servlet code, you can access all the standard Java APIs within the VM while your servlets are handling requests. This means that your Java servlet code can work well in a multitiered architecture, accessing "live" database connections with JDBC or communicating with other network services that have already been established. This kind of behavior has been hacked into CGI environments, but for Java, it is both robust and natural. Before we move on, we should also mention servlets' relationships to other technologies: Java Server Pages (JSPs), XML/XSL, and app frameworks such as Struts and Java Server Faces (JSF). We'll talk more about the technologies involved in web services later in the chapter.

JSPs

JSPs are a document-centric way to write server-side apps. They consist of HTML content with specialized tag libraries and active Java-like syntax embedded within the pages. JSPs are compiled dynamically by the web server into Java servlets and can work with Java APIs directly and indirectly to generate dynamic content for the pages. Although all of the work still occurs on the server side, JSPs allow the developer to work as if he were writing code right in the page, which has both benefits and drawbacks. The benefit of this sort of "immediate mode" coding style is that it is easy to grasp and quick to crank out. The drawback is that it can lead to an unmanageable mix of business logic and presentation logic. The more code that appears mixed in with the static content, the greater the maintenance headache. Most large-scale JSP projects today utilize tag libraries to minimize ad-hoc code in the pages. JSPs are also now routinely used in combination with controller servlets or other back-end framework controller components that do the heavy lifting of business logic for them. In this case, the term controller refers to the Model-View-Controller (MVC) separation of concerns that we introduced earlier when talking about Swing GUIs. Maintaining this separation leverages the advantages of JSP while avoiding its pitfalls.

XML

XML is a powerful set of standards for working with structured information in text form. The Extensible Stylesheet Language (XSL) is a language for transforming XML documents into other kinds of documents, including HTML. The combination of servlets that can generate XML content and XSL stylesheets that can transform content for presentation is a very exciting direction, covered in detail in . As we'll see later, web services use XML as their native data format, making them completely portable across platforms and languages.

app Frameworks

One of the most popular frameworks for building web apps is the Apache Foundation's Struts Web app Framework. Struts implements the Model-View-Controller paradigm by providing both a modular controller component architecture and an extensive tag library for JSP page development. Struts abstracts some of the mapping and navigation aspects required to glue together a web app through the use of an XML-based configuration file and also adds the ability to do declarative mapping of HTML forms to Java objects as well as automated validation of form fields. Over the years, some of the value of Struts has been whittled away by big improvements in the underlying Servlet API. As we'll see in this chapter, the web.xml web app configuration file can accomplish a lot by itself and features such as servlet filters and declarative features such as security and login have added a great deal to the modularity of servlet-based apps. Struts can also be a bit awkward and is not quite as clean or easy to understand as one might wish. However, for now Struts is the 800-pound gorilla in the room and commands a lot of attention. Struts has a big following and for many people it is an invaluable piece of the web infrastructure. You can download Struts at http://struts.apache.org/. JSF is Sun's response to Struts. Developed over the past few years by the Java Community Process (including some of the original Struts people) it is intended to become the "official" Java-sanctioned web-app framework. JSF builds upon lessons learned with Struts and refines the MVC model with server-side app components and more fine-grained navigation and event management. JSF is maturing after initially being received with mixed reviews. It is probably on track for wider adoption in the future. You can download the JSF APIs and learn more at http://java.oracle.com/j2ee/javaserverfaces/.

Comments