Solve your
servlet-based presentation problems - JavaWorld November
2000
Tutorial Details:
Solve your servlet-based presentation problems
Solve your servlet-based presentation problems
By: By Kevin Unger
Various techniques for implementing content presentation in thin-client applications
un Microsystems created Java servlet technology, and it was good. (See Resources .) All subsequent Java server techniques have been based on servlets. And while nobody argues against servlets' efficacy, the debate rages on over which follow-on technologies (if any) are best for solving Web-based presentation problems. Those problems include:
Separating presentation (HTML) from content generation code (code that accesses databases and generates XML documents, result-set data structures, Java object data, and so on)
Maintaining consistency with the modern component-based software construction paradigm, to leverage modularization and code reuse
Reuse and nesting of markup in different page contexts (templating)
Minimizing the learning curve for new scripting languages and tag vocabularies, and maximizing their compatibility with HTML authoring tools
Combining code and presentation obfuscates the design intent behind both elements, and encourages the designer/programmer to design the software and the page simultaneously -- two very distinct activities. Or, even worse, if separate page designers and programmers work on the site, they won't be able to work simultaneously without breaking each other's code.
Disregard for object-oriented software-component construction and the inability to reuse and nest markup blocks are closely related to the blending of markup with content generation code. Mingling code and markup means specific content generation code is hardwired to specific formatting. That limits your ability to build encapsulated objects for generating content. There is a similar problem for presentation components (templates): if the presentation is mingled with the content generation code, you have little chance of reusing or nesting page elements.
The techniques discussed in this article (besides raw servlets) enhance HTML documents by employing scripting languages, sets of custom markup tags, and automatic Java class generation to help solve the first three problems in the above list. If it is too difficult for nonprogrammers to learn to use these tools, then we must still deal with the problem of programmers being forced to become page designers. Page designers rely on HTML authoring tools, so new tags and scripts should be as friendly to authoring tools as possible.
I did not include performance and scalability in the list of problems, but they are primary issues. Commercial servlet engines generally provide excellent performance and most now offer high-end scalability features, so most servlet-based presentation solutions will perform and scale well. However, some techniques add loads to the server, such as realtime XML parsing and XSL processing. The performance-analysis details of adding those loads are beyond the scope of this article.
The following discussion describes some servlet-based Webpage-presentation techniques, with respect to the above list of problems. Specifically, this article discusses the main techniques for solving presentation problems by focusing on a small, representative sampling of systems that implement them. A raft of servlet-based templating engines and server-side Java application frameworks are available or in development, but are not mentioned here.
Raw servlets
Servlets are a vast improvement on the heavyweight process-per-request programming model used by CGI scripts. The servlet model of handling requests using lightweight Java threads (one per client request) is much more efficient. The mini-programs also add a degree of persistence across requests, without using a formal database or disk storage. Servlets remain the mainstay on the Java server for managing client requests.
However, trading in CGI scripts for Java servlets does not address the problem of the HTML deeply embedded within the code -- be it Perl or Java -- that generates the page content. Servlets are commonly used to dynamically generate HTML pages for interactive Websites by aggregating the HTML text and writing it to a java.io.PrintWriter object. This tedious process couples presentation and content generation. (Perl's text manipulation prowess actually makes it the better language for this.) As a direct result, either the page designer must learn Java programming, or the Java programmer must learn page design.
Building presentation components using raw servlets is conceivable, especially if the design included some mechanism for using and reusing blocks of HTML, into which dynamic content could be embedded at request time. The rest of the techniques rely on the addition of some form of templating to raw servlets. You can consider these as alternatives to a roll-your-own approach to Webpage templates.
Templating engines
Templating is a technique that enhances static HTML documents by adding either scripting code or custom tags, or by automating the creation of Java classes that represent the documents. A template, then, is a reusable piece of enhanced HTML that, through Java manipulation or embedded scripting, can facilitate the runtime inclusion of dynamically generated content. The rest of this section briefly describes three exemplary open source templating engines.
WebMacro
WebMacro provides a minimal framework for implementing the Model/View/Controller (MVC) architecture in a Web context. (See Resources for links.) The framework facilitates the mapping of requests to and the execution of templates, and provides them with session information and any business object data they need to access. The scripting language allows the templates to randomly access the business objects. In MVC parlance, the business object data comprise the model, the templates are the views, and the code that maps the requests to and executes the templates is the controller.
WebMacro's core feature is its support for a simple scripting language (with a syntax resembling Perl's). Actually, WebMacro's creator, Justin Wells, calls it a "templating language" to distinguish it from a full-fledged scripting language. The language supports:
Primitive branching
Iterating through lists of objects
Access to data generated by external Java objects
Inclusion of other template files or any text file (static include)
Parsing of template files (dynamic include)
Local variables
WebMacro uses a deep Java reflection (see Resources ) process to ascertain the topography of objects. This gives the templates access to data without the need for a special syntax to declare objects, bind local identifiers to object classes and properties, or get and set properties. A template can simply refer to a deeply embedded field (or a method) with dot-separated identifiers that match the object's field and method identifiers.
The most straightforward way to implement a service using WebMacro is to subclass Webmacro's WMServlet class and override the handle() method, which takes a request Context object and returns a Template object:
org.webmacro.Template handle(org.webmacro.servlet.WebContext)
The handle() function implements these steps:
Use the WebContext information (including the HttpServletRequest object) to determine which template to use
Put any data the selected template needs into the context
Get the parsed template -- cache it if it's not yet in the cache
Return the template to WebMacro, which will execute and render it back to the client
WebMacro completely removes HTML from Java code and embeds server-side scripting in the HTML files, transforming them into WebMacro template files. This makes the HTML more complex, and at least marginally reduces the utility of HTML-authoring tools. The added overhead for HTML authors is presumably outweighed by the programmer productivity gained by relieving you of the burden of generating HTML. WebMacro's simple, yet flexible, templating supports template reuse, and the dynamic-include feature supports template nesting.
The addition of a scripting -- sorry, "templating" -- language introduces a learning curve for HTML designers. WebMacro minimizes this problem by keeping the language primitive, but capable enough to handle the types of coding needed in a template.
WebMacro has recently been reimplemented by the Apache Software Foundation's Jakarta project (see Resources ), and branded as the "Velocity" subproject.
FreeMarker
FreeMarker (see Resources ) is remarkably similar to WebMacro. It implements MVC exactly as was described for WebMacro. The main differences are:
FreeMarker's scripting language is more sophisticated than WebMacro's: it has functions and Boolean expressions
FreeMarker's scripting language has a more heavyweight syntax
FreeMarker's template-caching mechanism has automatic cache updating, so modified templates can be automatically incorporated without bringing down the service
Both systems address the four presentation problems admirably, but WebMacro is better. A templating system's main purpose is to separate programming from presentation, so the benefits of FreeMarker's more sophisticated scripting capabilities are dubious -- especially if you consider presentation problems 1 and 4, listed previously. WebMacro's lack of "powerful" scripting features is an advantage, not a weakness.
FreeMarker's cache-updating feature is primitive, and both systems' caching is based on a filesystem. Neither system implements a database-caching mechanism, although both allow you to add one.
XMLC
I've contrasted the way raw servlets support dynamic pages -- by embedding HTML inside the Java code -- with the way the WebMacro and FreeMarker templating engines embed some form of scripting inside HTML files. XMLC's approach is completely different. (See Resources .) XMLC is a specialized Java compiler for HTML (or XML) template files. That's right, XMLC compiles pure HTML fi
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Solve your
servlet-based presentation problems - JavaWorld November
2000
View Tutorial: Solve your
servlet-based presentation problems - JavaWorld November
2000
Related
Tutorials:
XSLT blooms with
Java
XSLT blooms with
Java |
Use Web services
to integrate Web applications with
EISs
Use Web services
to integrate Web applications with
EISs |
Step into
the J2EE architecture and process
Step into
the J2EE architecture and process |
Rumble in the
jungle: J2EE versus .Net, Part
1
Rumble in the
jungle: J2EE versus .Net, Part
1 |
Repair invalid cached services in the Service Locator pattern
Repair invalid cached services in the Service Locator pattern |
Enhance your J2EE presentation layer
Enhance your J2EE presentation layer |
Isolate server includes' runtime context
Isolate server includes' runtime context |
Good article on how to adopt XP
Good article on how to adopt XP
xtreme programming (XP) is a software development methodology that makes coding the primary activity. By promoting values such as simplicity and feedback, XP allows Java programmers to incrementally develop and test applic |
FindBugs, Part 1: Improve the quality of your code
FindBugs, Part 1: Improve the quality of your code
One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren't really problems -- that is, false positives. When false positives occur, developers learn |
Inside Class Loaders: Debugging
Inside Class Loaders: Debugging
This article will show how to solve class-loading problems and to overcome some debugging limitations of the JDK class loaders.
|
YourKit Java Profiler 2.5.2 Released
YourKit Java Profiler 3.2 Released
With help of YourKit Java Profiler, an outstanding tool for Java professionals, you can easily solve wide range of CPU and memory related performance problems in J2EE and J2SE applications.
|
Solving the logout problem properly and elegantly
Summary
Properly handling the logout process in a password-protected Web application requires more than just calling the invalidate() method on the HttpSession object because most modern browsers, with the Back and Forward buttons, allow users to go back |
YourKit Java Profiler 3.2 Released
With help of YourKit Java Profiler, an outstanding tool for Java professionals, you can easily solve wide range of CPU and memory related performance problems in J2EE and J2SE applications. |
JTimepiece
JTimepiece is the advanced library for working with dates and times in Java. Many easy-to-use methods in this API make it easy for any developer, from beginner to expert, to use JTimepiece. |
alaJSP JSP-similar processor
It is yet another servlet based preprocessor. The common idea behind that line of the products (see ColdCafe site) is splitting static HTML presentation which done by designers and dynamic proceed developed by programmers. |
J2EE pathfinder: Implement JSP custom tags in five easy steps
JSP custom tags provide a standardized mechanism for separating presentation and business logic in a dynamic Web page, allowing page designers to focus on presentation while application developers code the back end. In this installment of J2EE pathfinder, |
Bioinformatics
Bioinformatics
Bioinformatics
Introduction
Bioinformatics is new hot topic after the Software. In the coming days there will be huge demand of Bioinformatics professionals in all sectors of biotechnology, pharmaceutical, and biomedical sciences. |
Techniques used for Generating Dynamic Content Using Java Servlets.
Techniques used for Generating Dynamic Content Using Java Servlets.
Techniques used for Generating Dynamic Content
Common Gateway Interface (CGI)
For any web application high performance and timely delivery are key ingredients to competitive |
Chat Transcript: Solving the Device Fragmentation Problem
Read the questions that your fellow developers had about the new feature in NetBeans Mobility Pack 4.0 that helps solve device fragmentation problems, and the answers straight from the engineers who created the module. |
New Technical Articles: 64-bit Programming on Solaris 10 OS for x86 Platforms
Four technical articles describe the new Sun Studio 10 software's 64-bit programming features on the Solaris 10 OS for x86 and AMD64 platforms. Important issues regarding the AMD64 ABI (Application Binary Interface), debugging, migration to 64-bits, and p |
|
|
|