站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Echo指南(二)

Echo指南(二)

摘要: Echo指南(二)

配置你的开发环境
注意:本章只和用Echo配置你自己的应用程序相关。如果你只打算使用没有修改的指南示例程序,只需要阅读前一章《建立示例程序》就足够了。
要安装Echo,你需要一个符合Java Servlet 2.2 规范的Java Servlet Container。如果你还没有的话,我们建议你使用开源的自由软件Jakarta Tomcat servlet container的4.0和以上版本。你可以从jakarta.apache.org下载到它。任何J2EE 1.2应用程序服务器都将提供Java Servlet 2.2 container。
创建应用程序
如果你想建立你自己的应用程序,你首先需要下载Echo的最新稳定版本。解压zip(或者是tar.gz)包,将Echo.jar 和 EchoServer.jar放到lib/子目录下的。在你编译和运行Echo程序时,这两个文件必须对java虚拟机可用。
如果你要将你的程序打包成Web包(WAR file):
复制Echo.jar 和 EchoServer.jar 文件到你项目的 WEB-INF/lib 目录。
如果你不是:
确定Echo.jar 和 EchoServer.jar 文件在你程序运行时环境的CLASSPATH 下可用。
在你的程序中分发和使用Echo.jar 和 EchoServer.jar是被允许的 。(包括商业程序和不开源的程序)
Hello, World!
沿用由Kernighan 和 Ritchie在他们的The C Programming Language中开创的历史悠久的传统,第一个Echo示例程序将在浏览器显示“Hello, World!”对所有实际的目的来说,这是所能建立的最简单的Echo应用程序了。下边的屏幕显示了Hello World 程序很普通的输出。
Example browser session of the Hello, World! example.
如果你已经安装了Echo指南示例程序,你可以访问
http://localhost:8080/EchoTutorial/helloworld来运行这个示例。如果你的服务器并不是在localhost的8080端口运行,一定要记得修改hostname和端口号。
所有的Echo程序都将最少包含两个类。第一个,必须继承Echo的 EchoServer类用来创建一个新的允许每个用户访问的Echo应用程序的“用户实例”。第二个类是应用程序自己的用户实例,它必须继承Echo的EchoInstance类。
Hello World 只包含了必须的两个类。第一个是继承自EchoServer的HelloWorldServlet。EchoServer只为用户提供了一个newInstance()方法,newInstance()方法的目的是在一个新用户第一次访问Echo应用程序的时候创建一个唯一的用户实例。和在示例程序中看见的一样,newInstance()只简单的返回HelloWorld 对象的一个实例。EchoServer 继承Java Servlet container的 HttpServlet 类没有任何实际意义,它只是使Echo程序能在所有的Servlet container上运行。
这个例子的第二个类定义了Hello World 程序本身。这个叫HelloWorld的类继承自EchoInstance。EchoInstance代表了Echo程序的唯一一个用户实例。用户又只被要求提供一个方法:init()。init()方法相当于桌面程序的static main(String[] args)初始化方法。
init()方法必须返回一个Echo Window 对象。Window 对象代表了用户浏览器窗口的内容。当用户访问Echo程序时,程序将在用户打开的浏览器窗口中显示内容。这个打开的窗口将由返回的Window 对象表示。
HelloWorldServlet.java
import nextapp.echo.ContentPane;
import nextapp.echo.EchoInstance;
import nextapp.echo.Label;
import nextapp.echo.Window;
import nextapp.echoservlet.EchoServer;
public class HelloWorldServlet extends EchoServer {
// Returns a new user-instance of the Echo application.
public EchoInstance newInstance() {
return new HelloWorld(); } }
class HelloWorld extends EchoInstance {
// This init method is called when a user first visits the
// application. It must return a Window object that will
// occupy the contents of the user's open browser window.
public Window init() {
// Create a new window.
Window window = new Window();
// Create a content pane. Components may not be added
// directly to a window, only to a content pane.
ContentPane content = new ContentPane();
// Set the window's content to be the content pane.
window.setContent(content);
// Create a new label that says "Hello, World!"
Label label = new Label("Hello, World!");
// Add the label to the content pane.
content.add(label);
// Return the new window.
return window; } }
--
PS: 刚开始翻译东西,翻得不好大家见谅了。
↑返回目录
前一篇: Echo指南(一)
后一篇: echo 使用手册