bha*_*avs 5 html javascript java embedded-jetty
我是网络开发的新手,也是使用嵌入式码头的新手.下面介绍的源代码是使用eclipse IDE开发的.我必须以编程方式启动jetty服务器,我没有通过命令行启动它的选项.它需要是一个极轻量级的Web界面,因为它将从具有低内存/处理速度的系统启动.
我在ECLIPSE中开发了以下目录结构
JettyExample <Project>
src
sample_package
HelloWorld.java
WEB-INF
index.html
web.xml
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
index.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>
Run Code Online (Sandbox Code Playgroud)
web.xml文件是通常的欢迎文件列表.当我运行服务器并在Web浏览器中启动localhost:8080时,我收到404错误我不知道我需要添加到web.xml文件是什么,或者web.xml文件的引用不是在HelloWorld.java主方法中更正.
任何提示/建议都会有所帮助编辑1:
我在类路径中包含了server-api.jar文件和jetty.jar文件,而没有使用eclipse的Maven插件.
EDIT2:
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
Run Code Online (Sandbox Code Playgroud)
您已将欢迎文件设置为 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" });
Run Code Online (Sandbox Code Playgroud)
如果您仅包含index.html文件作为欢迎文件,并且还确保index.html位于应用程序的根目录中,那么Jetty服务器应该能够找到它:
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
Run Code Online (Sandbox Code Playgroud)
请务必在进行此更改后重新启动 Jetty,因为应用程序需要重新加载此信息。
此外,当在服务器上配置新的 Web 应用程序时,通常最好将日志记录级别一直调高。服务器和框架通常在较低级别进行日志记录,因此不会干扰应用程序日志;但是,在这种情况下,您需要查看当您在浏览器中加载 localhost:8080 时 servlet 容器尝试访问哪些资源。
为了进一步澄清,ResourceHandler.setWelcomeFiles Java 方法与在非嵌入式 Jetty 中通过 web.xml 配置服务器相同,使用以下 XML 条目:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
Eclipse Wiki 页面上的 Embedding Jetty提供了一些示例和更多文档,请务必查看它们以获取更多指导。
嵌入式Jetty 6的文件结构:
这是我拥有的嵌入式 Jetty 副本的示例文件结构。请注意,index.html 位于根目录中,就在 src 旁边:
build.properties* index.html* README.textile* src/ war/
build.xml* licenses/ server/ test/ WEB-INF/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7346 次 |
| 最近记录: |