0

As you can see the image below, My HTML index page is at static folder. How can I point localhost to static/index.html

https://i.stack.imgur.com/r9veL.png

Tried on my own on basic hello world.

static class MyHandler implements HttpHandler { 
    public void 
    handle(HttpExchange t) throws IOException { 
    String response = "Hello world"; 
        t.sendResponseHeaders(200, response.length()); 
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes()); os.close(); 
} 
}

I need to know how I can point the default localhost:8080 to my pre-built existing static/index.html page?

Syfer
  • 4,262
  • 3
  • 20
  • 37
sussie
  • 29
  • 1
  • 1
  • 5

1 Answers1

0

If I may have understood ur question correctly on the start up of your application you want to display your page as a default page please go through below link
How to configure welcome file list in web.xml
This will let you know how to configure your web.xml page to let tomcat or any other web container to know your default page.

you may try this

public class Test {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/welcomePage", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "This is the response";
            // we do not set content type, headers, cookies etc.
        // resp.setContentType("text/html"); // while redirecting as
        // it would most likely result in an IllegalStateException

        // "/" is relative to the context root (your web-app name)
        RequestDispatcher view = req.getRequestDispatcher("/path/to/file.html");
        // don't add your web-app name to the path

        view.forward(req, resp);
        }
    }
}
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37