如下图所示,我的HTML索引页面位于静态文件夹中。如何将localhost指向static/index.html
在我自己的基本问候世界中尝试过。
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();
}
}
我需要知道如何将默认的localhost:8080指向我预先构建的现有static / index.html页面?
答案 0 :(得分:0)
如果我在启动您的应用程序时可能已正确理解您的问题,您希望将您的页面显示为默认页面
请通过以下链接
How to configure welcome file list in web.xml
这将告诉您如何配置web.xml页面以让tomcat或任何其他Web容器知道您的默认页面。
你可以试试这个
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);
}
}
}