@title("Create a New Web Action")
[index.html Top Page]
=Create a New Web Action
UTGB track (web application) consists of a set of actions, which are accessible from http://localhost:8989/myapp/(action name). To add your own action, type {b|utgb action} (action name). It will add a new action into you web application. To enable the new action in your web server, you need to issue the compile command again, and have to restart the web server.
{{
myapp> utgb action HelloWorld
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).
==Magic in Web Actions
A web action receives a set of URL query parameters in the form of String. For example, a request 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 type of these values; for example, the name value is String itself, and the year value must be translated from String to Integer. To program these processes is bothersome and usually error-prone. In UTGB, these tasks can be performed in a simple manner. All you need to do is to add appropriate setter methods in your action. UTGB automatically translates String input values in a URL request by investigating the setter's argument type.
public class HelloWorld extends WebTrackBase
{
private static final long serialVersionUID = 1L;
private static Logger _logger = Logger.getLogger(HelloWorld.class);
private String name = "World";
private int year = 2007;
public HelloWorld()
{}
public void handle(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.getWriter().println("Happy new year! " + name + ":" + year);
}
// setters for year and name parameters
public void setYear(int year) { this.year = year; }
public void setName(String name) { this.name = name; }
}