Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.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();

}

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 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:

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
like image 741
bhavs Avatar asked Nov 03 '22 23:11

bhavs


1 Answers

You've set your welcome 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.setWelcomeFiles(new String[]{ "WEB-INF/index.html" });

If you include just the index.html file as a welcome 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.setWelcomeFiles(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.setWelcomeFiles Java method is the same as configuring the server via web.xml in non-embedded Jetty, using the following XML entry:

    <welcome-file-list>
            <welcome-file>index.html</welcome-file>
    </welcome-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/
like image 124
jmort253 Avatar answered Nov 11 '22 05:11

jmort253