使用嵌入式码头创建 Web 界面

using embedded jetty to create a web interface

本文关键字:Web 界面 创建 码头 嵌入式      更新时间:2023-09-26

我是Web开发和使用嵌入式码头的新手。下面介绍的源代码是使用 eclipse IDE 开发的。我必须以编程方式启动码头服务器,我没有通过命令行启动它的选项。它需要是一个非常轻量级的Web界面,因为它将从内存/处理速度较低的系统启动。

我在ECLIPSE中开发了以下目录结构

  JettyExample <Project>
    src 
     sample_package
        HelloWorld.java
     WEB-INF
      index.html
      web.xml

HelloWorld的源代码.java

 public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase(args.length == 2?args[1]:".");
    resource_handler.setWelcomeFiles(new String[]{ "WEB-INF/index.html" });

    System.out.println("serving " + resource_handler.getBaseResource());
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);
    server.start();
    server.join();
}

索引.html 是

 <html>
<head>
    <title>HTML Generator Sample Page</title>
</head>
<body>
    <h1 style="text-align: center;">
        Agent Management Interface</h1>
    <ol>
        <li>
            Start Platform</li>
        <li>
            Show Agent Status</li>
        <li>
            Create Dummy Agent</li>
        <li>
            Intiate Request Message</li>
        <li>
            Stop agent</li>
        <li>
            Stop Platform</li>
    </ol>
    <p>
        Enter option :</p>
    <p>
        <textarea cols="10" name="myTextBox" rows="1" style="width: 104px; height: 25px;"></textarea></p>
    <p>
        <input name="option_selector" type="submit" value="option_selector" /></p>
</body>

Web.xml 文件是通常的文件,其中包含欢迎文件列表。当我运行服务器并启动时本地主机:8080 在网络浏览器中,我收到 404 错误我不确定我需要添加到 web.xml 文件中的内容,或者 HelloWorld.xml main 方法中对 web 文件的引用不正确.java。

任何提示/建议都会有所帮助编辑 1:

我在类路径中包含服务器 api.jar 文件和 jetty.jar 文件,并且没有使用 Maven 插件进行 eclipse。

编辑2:

2012-05-25 14:40:39.253:DBUG:oejs.AsyncHttpConnection:async request (null null)@17160330 org.eclipse.jetty.server.Request@105d88a
2012-05-25 14:40:39.260:DBUG:oejs.Server:REQUEST / on   org.eclipse.jetty.server.nio.SelectChannelConnector$SelectChannelHttpConnection@1db05b2@127.0.0.1:8080<->127.0.0.1:55062
2012-05-25 14:40:39.264:DBUG:oejs.Server:RESPONSE /  200
2012-05-25 14:40:39.267:DBUG:oejs.AsyncHttpConnection:async request (null null)@17160330 org.eclipse.jetty.server.Request@105d88a
2012-05-25 14:40:39.272:DBUG:oejs.AsyncHttpConnection:async request (null null)@17160330 org.eclipse.jetty.server.Request@105d88a
2012-05-25 14:40:39.273:DBUG:oejs.Server:REQUEST /jetty-dir.css on org.eclipse.jetty.server.nio.SelectChannelConnector$SelectChannelHttpConnection@1db05b2@127.0.0.1:8080<->127.0.0.1:55062
2012-05-25 14:40:39.275:DBUG:oejs.Server:RESPONSE /jetty-dir.css  404
您已

将欢迎文件设置为 WEB-INF/index.html。位于 WEB-INF 文件夹内的项仅对 servlet 容器可见,在容器外部不可访问。

这是行不通的,因为index.html隐藏在WEB-INF后面。此外,在使用 WEB-INF 时,通常从应用程序的根目录访问它,例如/WEB-INF/file.html:

resource_handler.setWelcomeFiles(new String[]{ "WEB-INF/index.html" });

如果仅包含 index.html 文件作为欢迎文件,并确保 index.html 位于应用程序的根目录中,则 Jetty Server 应该能够找到它:

resource_handler.setWelcomeFiles(new String[]{ "index.html" });

请务必在进行此更改后重新启动 Jetty,因为应用程序需要重新加载此信息。

此外,在服务器上配置新的 Web 应用程序时,通常最好将日志记录级别一直调高。服务器和框架通常在较低级别进行记录,因此它们不会干扰应用程序日志;但是,在这种情况下,您需要查看在浏览器中加载 localhost:8080 时 servlet 容器尝试访问哪些资源。

为了进一步澄清,ResourceHandler.setWelcomeFiles Java 方法与在非嵌入式 Jetty 中通过 web.xml 配置服务器相同,使用以下 XML 条目:

    <welcome-file-list>
            <welcome-file>index.html</welcome-file>
    </welcome-file-list>

Eclipse Wiki 页面上有一些关于嵌入码头的示例和更多文档,请务必查看它们以获取更多指导。

嵌入式 Jetty 6 的文件结构:

这是我拥有的嵌入式 Jetty 副本的示例文件结构。请注意,索引.html位于根目录中,紧挨着 src:

build.properties*  index.html*  README.textile*  src/   war/
build.xml*         licenses/    server/          test/  WEB-INF/