Wt: C++ Web Toolkit Library Lets You Write Scripting-Independent Web Apps

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Wt (pronounced ‘witty’) is a C++ library and application server for developing and deploying web applications. Although Wt supplies a GUI, it is not your typical “framework”, which locks you into someone’s preconceived idea of how applications should be structured. Rather, Wt is widget-centric, and although inspired by existing C++ GUIs, it offers complete abstraction of any web-specific implementation details, all the way down to event handling and graphics support. It’s probably not a coincidence that Wt is named similarly to the ever-popular Qt application development system!

In fact, none of today’s so-called “page-based frameworks” (built on PHP or JSP/JSF derivatives) for developing web applications make abstraction of the peculiarities of the underlying implementation technologies. As a consequence, a developer must gain familiarity with a panoply of ever-changing technologies, including HTML/XHTML, JavaScript, CSS, AJAX, CGI, DHTML, SVG/VML/Canvas—just to name a few. Moreover, as soon as you drive a stick into the ground and choose a technology such as AJAX or JavaScript, you must take responsibility for graceful degradation when these pieces are missing in action due to or disabled for local security reasons. Little has changed in the structure of applications that follow the primarily page-centric paradigm of 1990s HTML. This means that you will need to design and maintain manually your client-server communication when using advanced AJAX techniques.

Generating HTML code or filling HTML templates is prone to security problems such as Cross-Site-Scripting (XSS), by unwillingly allowing JavaScript to be inserted in the page. Ironically, template frameworks cannot avoid this because as a developer you need to be able to insert self-written JavaScript to improve your web application.

In contrast, a web application developed with Wt is written in only one compiled language (C++), from which the library generates the necessary HTML/XHTML, JavaScript, CGI, or AJAX code. The responsibility of writing secure and browser-portable web applications is handled by Wt. For example, if available, Wt will maximally use JavaScript and AJAX, but applications developed using Wt will also function correctly when AJAX is not available, or when JavaScript is disabled, reverting to a plain HTML/CGI mechanism for communication between browser and server.

Typical use scenarios:

  • Web-based GUIs for embedded systems
  • Web-based GUIs that require integration with (existing) C++ libraries
  • Creating a port of existing C++ desktop applications to the web

Some benefits of using Wt:

  • Develop web applications like you develop C++ desktop applications.
  • Provides plain widgets, which work regardless of JavaScript availability.
  • Enables more polished or advanced functionality when JavaScript is available (without re-coding)
  • Built-in HTTPD and FastCGI for easy development and deployment.
  • A single specification for both client- and server-side validation and event handling (when using stateless slot implementations).
  • Generates standards compliant HTML or XHTML code.
  • Portable, anti-aliased graphics with VML, SVG, or HTML 5.
  • No exposure of business logic, which stays at the server.
  • Page load time is limited only by screen complexity, not application size.

Installation

Installing Wt for Windows is a bit harder than implementing it on a Linux host, primarily because there isn’t anything as simple as Linux package management on Windows. So, it becomes a bit of a scavenger hunt, although there is a nice how-to document. Basically, you are responsible for locating the suggested versions of BoostPro Boost library (currently, 1.34.1). BoostPro requires asio, the asynchronous I/O model for C++. This is theoretically included in BoostPro 1.35 although I didn’t test it out. These components in turn require cmake, a freeware “make” type utility.

Hello Wt!

You’ll start off with a “hello world” type app that provides a simple CGI type form and see how this design can be realized by a C++ app. Unlike most apps that I write about, you can actually run it for yourself right now on the web! What you’ll see first is a classic web form presentation shown in Figure 1. After you enter your name and click the Submit button (“Greet me.”), it rewrites the page and more-or-less you are back to the initial state, as shown in Figue 2.

Figure 1: Hello world forms (entering text)

[Wt2.jpg

Figure 2: Hello world forms (after clicking Submit)

So, take a walkthrough of this program and see how it was done. First, keep in mind that most GUI “hello world” apps often run 50 to 100 lines of code.

 1 #include <WApplication>
 2 #include <WBreak>
 3 #include <WContainerWidget>
 4 #include <WLineEdit>
 5 #include <WPushButton>
 6 #include <WText>
 7
 8 using namespace Wt;
 9
10 class HelloApp : public WApplication
11 {
12 public:
13    HelloApp(const WEnvironment& env);
14
15 private:
16    WLineEdit *nameEdit_;
17    WText *greeting_;
18
19    void greet();
20 };

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read