I want to serve static html pages in / and /index.html
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
BASE_URL = http://locahost:8080/api/
StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
My question is where should i put the static file in
In your case, static data should be located in /static folder in root of your disk.
There are a couple of options where the static resources can be located:
you can serve static resources from your filesystem via absolute path (e.g. /www/static/):
StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
or embedded your resources into jar archive (e.g. /www/static.jar):
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")}));
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
or embed static resources inside your application jar:
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With