Java Tutorial - Java Script :
Creating a Tea Application:
After you have satisfied yourself that you have a working Tea servlet environment, you can embark on your first development effort. We start by implementing Tea’s Application interface. Following is the source code for HelloMy CityApplication.java:
package freej2ee;
import com.go.teaservlet.*;
import javax.servlet.ServletException;
public class HelloMyCityApplication implements Application
{
public HelloMyCityApplication()
{
super();
}
// Return an instance of the class that contains the functions
public Object createContext(
ApplicationRequest request,
ApplicationResponse response)
{
return new HelloMyCityContext(request);
}
public void destroy()
{
}
// Returns the class that contains the functions
public Class getContextType()
{
return HelloMyCityContext.class;
}
public void init(ApplicationConfig config) throws ServletException
{
}
}
This is a very basic Tea application and as such does not require any special processing in the init() and destroy() methods. In fact, the only function of HelloMyCityApplication is to serve as a factory for creating the Context class that follows. The TeaServlet is configured to look for this class by the TeaServlet.properties file. After successful compilation, be careful to place the compiled class file in the WEB-INF/classes directory. The Application class serves as a class factory to create Context classes. The Context class does the real work of generating the data that will be used to fill in the template at run time. Following is the Context class for our sample application:
package freej2ee;
import javax.servlet.http.*;
public class HelloMyCityContext
{
HttpServletRequest mRequest;
public HelloMyCityContext()
{
super();
}
public HelloMyCityContext(HttpServletRequest request)
{
// Save the user’s request object.
mRequest = request;
}
public String getMyCity()
{
// Get “myCity” parameter from the URL.
String myCity = mRequest.getParameter(“myCity”);
// If it is null, then make Fort Myers the default city.
if (myCity == null)
{
myCity = “Fort Myers ”;
return myCity;
}
}
The Context class HelloMyCityContext contains the methods that will be exposed to the HelloMyCity template for the purpose of delivering data to the template. After compiling this class be careful to place the compiled class file in the classes directory alongside your application class.
