Table of contents


Disclaimer

If me talking about servlet doGet(...) and doPost(...) methods gives you a glazed look in your eyes, this is probably not the right place for you to be. For the rest of you, please read on and enjoy.

Simple example

To illustrate the useage of the Servlet Widget Toolkit, lets take the classic Hello World example, and use it to show how to write a simple servlet interface using the traditional approach of windows gui widgets to construct the interface.
We shall deviate slightly from this timeless classic, only to make the "Hello World" text a clickable label.
I omit the details for deploying the servlet itself, if you want simply use the war file which is included in the download package for a more complete example of the capabilities of this code.
In particular, note the familiar event handler code towards the end of the listing.
The complete servlet listing follows in Listing 1.


import javax.swt.*;
import javax.swt.event.*;

public class Test extends HttpServlet implements ActionListener {
    private Window window;

public void init() {
setup(); 
}

public void setup() {
window = new Window(this, "Test");
window.setLayout(new BorderLayout());

Panel center = new Panel();
Label label = new Label("Hello world!");
center.add(label, BorderLayput.CENTER);
window.add(center, BorderLayout.CENTER);
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
window.notify(req, res);
window.paint(req, res);
}
   
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
window.notify(req, res);
window.paint(req, res);
}

public void actionPerformed(ActionEvent e) {
Component b = e.getSource();
System.out.println("actionPerformed on component "+b.getName()+" type="+e.getTypeDescription());
}
}

The only constraints are all widgets need to be associated with a Window, and you need to call the hooks in your servlet doGet(..) and or doPost(...) methods to correctly get the notifications and component painting events.

Session based or global?

You may be sharp enough to note that any changes to the widget, such as calling setText("new text") will cause all browsers which are viewing that widget to be changed accordingly, although this will only e evident at the next paint event. While this is quite powerfull, you can over-ride this behaviour, since all widgets are derived from javax.swt.Component, which has a method setGlobal(boolean global) which can specify that changes to a widget are limited to the session the widget is being viewed from.
This allows you to mix and match your widgets and get some powerfull side effects for sharing data between servlet sessions.