java - using embedded jetty to create a web interface - Stack Overflow

I am a newbie at web development, and at using embedded jetty. The source code presented below is devel

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
Add a ment  | 

1 Answer 1

Reset to default 2

You'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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745363589a4624472.html

相关推荐

  • java - using embedded jetty to create a web interface - Stack Overflow

    I am a newbie at web development, and at using embedded jetty. The source code presented below is devel

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信