Echo指南(二)

配置你的开发环境

 

注意:本章只和用Echo配置你自己的应用程序相关。如果你只打算使用没有修改的指南示例程序,只需要阅读前一章《建立示例程序》就足够了。

 

要安装Echo,你需要一个符合Java Servlet 2.2 规范的Java Servlet Container。如果你还没有的话,我们建议你使用开源的自由软件Jakarta Tomcat servlet container4.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来运行这个示例。如果你的服务器并不是在localhost8080端口运行,一定要记得修改hostname和端口号。

 

所有的Echo程序都将最少包含两个类。第一个,必须继承Echo EchoServer类用来创建一个新的允许每个用户访问的Echo应用程序的“用户实例”。第二个类是应用程序自己的用户实例,它必须继承Echo的EchoInstance类。

Hello World 只包含了必须的两个类。第一个是继承自EchoServerHelloWorldServletEchoServer只为用户提供了一个newInstance()方法,newInstance()方法的目的是在一个新用户第一次访问Echo应用程序的时候创建一个唯一的用户实例。和在示例程序中看见的一样,newInstance()只简单的返回HelloWorld 对象的一个实例。EchoServer 继承Java Servlet container HttpServlet 类没有任何实际意义,它只是使Echo程序能在所有的Servlet container上运行。

这个例子的第二个类定义了Hello World 程序本身。这个叫HelloWorld类继承自EchoInstanceEchoInstance代表了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: 刚开始翻译东西,翻得不好大家见谅了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值