5

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@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
bhavs
  • 2,091
  • 8
  • 36
  • 66
  • If you type in the whole path of the file ,is it still giving error to you? – BOSS May 25 '12 at 06: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 May 25 '12 at 06:45

1 Answers1

2

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/
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • thanks for the suggestion for changing the logging levels, even after this change, when I type localhost:8080 I now see the directory structure of my code base, when i click on src/WEB-INF folder i can see the html page I have created. I will update the question with the log messages from increasing the logging levels – bhavs May 25 '12 at 06:42
  • 1
    ok. move index.html into the same folder as src -- so they are siblings. Your index.html file is 2 levels deeper than where Jetty is looking. Also, the fact that WEB-INF is visible suggests that none of the configuration files are doing anything, since the container normally hides those by default. But that's okay since you're configuring programmatically instead of with XML files... – jamesmortensen May 25 '12 at 06:48
  • thanks for your suggestion this helped, I can view the page now – bhavs May 25 '12 at 07:00