English 中文(简体)
使用嵌入式码头创建 Web 界面
原标题:using embedded jetty to create a web interface

I am a newbie at web development, and at using embedded jetty. The source code presented below is developed using eclipse IDE. I have to start the jetty server programmtically, I do not have an option of starting it via the command line. It needs to be an extremely light weight web interface as it will be launched from a system with low memory/processing speed.

我在ECLIPSE中制定了以下目录结构:

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

Hello World.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>

the web.xml file is the usual one with a list of welcome files. when i run the server and launch localhost:8080 in the web browser, I am getting a 404 error I am not sure what is it that I need to add to the web.xml file or the referncing of the web.xml file is not correct in the HelloWorld.java main method.

Any hints/suggestions will be helpful EDIT 1:

我将服务器- api. jar 文件和喷气器. jar 文档纳入类路径, 而不是使用 Maven 插件来日食 。

内容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@[email protected]: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@[email protected]: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 文件夹内的项目只为服务器容器可见,无法进入容器外。

这将行不通,因为索引.html隐藏在WEB-INF后面。 此外,在与WEB-INF合作时,通常会从应用程序的根部(如/WEB-INF/files.html)上访问它:

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

如果您将索引. html文件仅列为欢迎文件, 并确保索引. html是您应用程序的根部, Jetty 服务器应该能够找到它 :

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

一定要在做出此更改后重新启动 Jetty, 因为应用程序需要重新装入此信息 。

此外,在服务器上配置一个新的网络应用程序时, 通常将您的日志水平一路调高是一个好主意。 服务器和框架通常在较低级别上进行记录, 这样它们不会干扰应用程序日志; 但是, 在此情况下, 您需要查看在您浏览器中装入本地主机: 808080 时服务器容器试图访问的资源 。

为了进一步澄清,ResourcesHandler.setWelpFiles Java 方法与使用以下XML条目通过网站.xml配置服务器相同:

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

”部分有一些例子和更多的文件,请务必查看,以获得更多的指导。

嵌入喷气6: < 强/强 > 嵌入喷气结构 < 强/强 >

以下是我拥有的嵌入的 Jetty 副本的示例文件结构。 请注意, 索引. html 位于根部, 就在 src 旁边 :

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

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签