When to use applets instead of HTML forms: Three easy techniques

Using applets for web page menus and database interactions

Web developers are plagued by the inconsistent browser rendering of HTML and JavaScript, yet most continue to use HTML forms to build GUI front-ends. In this article returning JavaWorld contributor Dr. Mark O. Pendergast goes against the grain, suggesting three cases where Java applets are a viable alternative to HTML forms. First learn how to use applets to create complex multi-level menus, then consider what you could gain from replacing HTML GET and POST operations with client- and server-side applet implementations. Note that security concerns are addressed for each of the solutions discussed in this article.

Web developers know too well the pain of trying to make web pages display consistently and scripts respond properly across multiple browsers. The problem is that the HTML/CSS/JavaScript specifications are constantly in flux, and so are their corresponding browser implementations. The W3C controls the specifications, browser developers interpret them, and web developers are stuck in the middle. In just the last year at the university where I teach, I've seen a half dozen advisories from our campus IT group about browser incompatibilities with the online learning management system. Close inspection of the code behind many websites reveals complex conditional logic to handle unpredictable variations in browser behavior. There must be a simpler way to display information and process forms.

Unlikely as it may seem to developers swayed by the hype about HTML 5 and JavaScript, I make the case in this article for using Java applets instead of HTML forms. Applets will enable you to create multi-level drop down menus, use a myriad of colors and styles, create forms with user-friendly controls, and communicate with web and database servers. And you can do all of this securely, using your favorite JVM language and IDE.

What about applet security?

The recent zero-day security flaw (discovered in Java 7 update 10 and subsequently patched by Oracle) allowed applets to bypass the Java security sandbox, granting themselves permission to execute arbitrary code. A frenzy of fear about Java security followed.

The truth is that Java is no different from Active X, Flash, and other plugins, in that it poses an occasional security threat. It was always possible for hackers to create self-signed applets, and it is also possible to secure your Java applications against them; not perfectly, but sufficiently. Rather than swearing off applets, I would hope that developers would take the time to educate themselves and strengthen the security of their Java applications. This article offers tips for writing secure applets in real-world development scenarios.

Applets for web development

Every Java programmer has created a Java applet, most of them in the category of simple clocks or calculators. Some of us have even played with fonts and colors and seen that it's possible to integrate our applets with web pages. But for some reason, developers tend to shy away from applets when it comes to creating serious applications that interact with a database. Most of the time, we choose HTML forms for user interactions involving stored data. It could be that we're leery of the security limitations placed on applets. Some of us got caught up in the Ajax frenzy; for others, it was just the technique that was taught in school. Whatever the reason the potential of using Java applets to build the GUI interface for web applications tends to be overlooked. With this article I aim to correct that oversight, by demonstrating three useful applications of applets for web development:

Along the way, you'll learn how to do all of the following with applets:

  • Program an applet to display a URL in the browser
  • Encode user input into the URL so that parameters are passed to the web server
  • Dynamically generate JavaScript to pass data back to the applet from the web server
  • Open a two-way stream connection (HTTPURLConnection) with a servlet in order to transmit entire objects

You can download the source code for this article anytime. The source package consists of three NetBeans projects:

  • The MenuApplet project shows a simple News-Weather-Sports applet-based menu system.
  • Projects in the URLEncoding folder contain an applet that demonstrates search, delete, add, and update functions for records in a database using the get method. The GUI for this applet is a JTabbedPane that holds forms for each function and utilizes JFormattedFields, InputVerifiers, and JTables.
  • Projects in the ServletStream folder have the same interface and perform the same functions as the URLEncoding projects, but they use the post method. All of the applets for these projects are unsigned and do not require any special security options, for reasons that I'll explain.

Before I get into the mechanics of these examples let's review some basics of deploying Java applets.

1 2 3 4 5 6 Page
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more