Top Page

Create a New Web Action

UTGB track (web application) consists of a set of web actions, accessible from http://localhost:8989/myapp/(action name). To add your own action, type utgb action (action name), which creates a new web action code into you web application source code. To enable the new action in the web browser, you need to recompile the project and then restart the web server.

myapp> utgb action HelloWorld     // will be generated

myapp> utgb compile

myapp> utgb server

You may see the new action from http://localhost:8989/myapp/HelloWorld or http://localhost:8989/myapp/helloworld (lower case name). You can write any Java code to generate web contents including plain text, HTML, PNG, JPEG graphics, etc., inside the handle() method in your web actions.

Binding URL query parameters to public fields in Web Action

Web action receives a set of URL query parameters, in the form of String data type. For example, an web request URL http://localhost:8989/myapp/helloworld?name=leo&year=2008 has two parameters, name and year. In the traditional web application development (e.g. CGI), you have to convert the data types of these values; for example, the name value has a String type, so you can use it as is, but the year value must be converted from the String to an Integer type. Programming such data type conversion is bothersome and usually error-prone. In the UTGB, such data type mappings between URL requests and web actions are supported.

All you need is to add public fieds in a web action class for recieving URL request paremeters. The UTGB automatically translates String input values in a URL request by investigating the public field parameter types.

public class HelloWorld extends WebTrackBase
    // private fields can be kept secret
    private static final long    serialVersionUID    = 1L;
    private static Logger _logger = Logger.getLogger(HelloWorld.class);
    // public fields will be modified according to the web request parameters
    public String name = "Leo";
    public int year = 2007;
    public void handle(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
        // You can use the name and year parameters, recieved from the URL request.
        // The following code displays "Happy New Year! Leo (2007)" if no parameter is set
        response.getWriter().println(String.format("Happy New Year! %s (%d)", name, year));

Supported Data Types for Data Binding

You can use following data types as public fields in your web action:

  • Primitive types: char, byte, int, long, float, double, boolean
  • Standard types: String, Date, File

Drawing Graphics

You can draw graphics using Graphics2D and ImageIO. For drawing graphics in Java, read a nice tutorial in

public void handle(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // set the content type to be generated to PNG image

    // prepare a buffer for drawing grpahics  
    BufferedImage buffer = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) buffer.getGraphics();

    // draw a rectangle
    g.fillRect(10, 50, 100, 150);
    // output the buffered image as PNG image 
    ImageIO.write(buffer, "png", response.getOutputStream());