admin管理员组文章数量:1425798
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 mand line. It needs to be an extremely light weight web interface as it will be launched from a system with low memory/processing speed.
I have developed the following directory structure in ECLIPSE
JettyExample <Project>
src
sample_package
HelloWorld.java
WEB-INF
index.html
web.xml
The source code of 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.setWeleFiles(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();
}
index.html is
<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 wele 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:
I am including the server-api.jar file and the jetty.jar file in the classpath and not making using of the Maven plugin for eclipse.
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@[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
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 mand line. It needs to be an extremely light weight web interface as it will be launched from a system with low memory/processing speed.
I have developed the following directory structure in ECLIPSE
JettyExample <Project>
src
sample_package
HelloWorld.java
WEB-INF
index.html
web.xml
The source code of 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.setWeleFiles(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();
}
index.html is
<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 wele 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:
I am including the server-api.jar file and the jetty.jar file in the classpath and not making using of the Maven plugin for eclipse.
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@[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
Share
Improve this question
edited May 25, 2012 at 6:44
bhavs
asked May 25, 2012 at 6:15
bhavsbhavs
2,3019 gold badges39 silver badges70 bronze badges
2
- If you type in the whole path of the file ,is it still giving error to you? – BOSS Commented May 25, 2012 at 6:24
- @abhishek bose I can see the directory structure, only when I type localhost:8080/src/WEB-INF i can see the html page from the index.html page – bhavs Commented May 25, 2012 at 6:45
1 Answer
Reset to default 2You've set your wele file to WEB-INF/index.html. Items that are located inside the WEB-INF folder are only visible to the servlet container and are not accessible outside of the container.
This will not work, since index.html is hidden behind WEB-INF. Additionally, when working with WEB-INF, it's customary to access it from the root of the application, such as /WEB-INF/file.html:
resource_handler.setWeleFiles(new String[]{ "WEB-INF/index.html" });
If you include just the index.html file as a wele file, and also make sure index.html is in the root of your application, the Jetty Server should be able to find it:
resource_handler.setWeleFiles(new String[]{ "index.html" });
Be sure to restart Jetty after making this change, since the application will need to reload this information.
Also, when configuring a new Web application on a server, it's generally a good idea to turn your logging levels all the way up. The server and frameworks typically log at lower levels so they don't interfere with application logs; however, in this case, you need to see what resources the servlet container is trying to access when you load localhost:8080 in your browser.
To clarify further, the ResourceHandler.setWeleFiles Java method is the same as configuring the server via web.xml in non-embedded Jetty, using the following XML entry:
<wele-file-list>
<wele-file>index.html</wele-file>
</wele-file-list>
There are some examples and more documentation at the Eclipse Wiki Page on Embedding Jetty, be sure to check them out for more guidance.
File structure of embedded Jetty 6:
Here is an example file structure of a copy of embedded Jetty that I have. Note that the index.html is in the root, right next to src:
build.properties* index.html* README.textile* src/ war/
build.xml* licenses/ server/ test/ WEB-INF/
本文标签: javausing embedded jetty to create a web interfaceStack Overflow
版权声明:本文标题:java - using embedded jetty to create a web interface - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745363589a2655407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论