Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从http服务器、filehandler中删除冗余代码_Java_Http_Handler - Fatal编程技术网

Java 从http服务器、filehandler中删除冗余代码

Java 从http服务器、filehandler中删除冗余代码,java,http,handler,Java,Http,Handler,我已经在8080端口设置了我的Web服务器,目前只是本地主机,用于测试 public class WebServer { static int port = 8080; //not the final port static String ip = "127.0.0.1"; // not the final IP public static void main(String[] args) throws IOException { InetSocketAddress i = ne

我已经在8080端口设置了我的Web服务器,目前只是本地主机,用于测试

public class WebServer {

static int port = 8080; //not the final port
static String ip = "127.0.0.1"; // not the final IP


public static void main(String[] args) throws IOException {

    InetSocketAddress i = new InetSocketAddress(ip, port); //localhost - 127.0.0.1
    HttpServer server = HttpServer.create(i, 0);

    server.createContext("/tester", new testHandler("index.html"));

    server.createContext("/startpage", new PagesHandler());
    server.createContext("/online", new OnlineHandler());
    server.createContext("/logfile", new LogFileHandler());

    server.setExecutor(null);
    server.start();

}
对于每个页面引用,我都有一个单独的处理程序,它基本上做相同的事情。 PagesHandler()的示例

这使得我有很多冗余代码,我真的不认为有必要。 我的问题是。我猜有一种方法可以用文件名来解析处理程序,而不是用文件中的硬编码文件名

但是我该怎么做呢? 具有字符串参数作为文件名的处理程序的构造函数会这样做吗? 像这样

大体上是这样的:

 server.createContext("/tester", new testHandler("index.html"));

如果没有,我将如何减少粗鲁的代码?

是的。这就是你可以做到的。好吧,但是在我的测试中,我似乎无法获得我试图获取的index.html文件。我得到了一个空指针异常?Editite-不是空指针异常,而是filenotfound异常,因为文件名为空。java.io.FileNotFoundException:public/null(无此类文件或目录)
public pagesHandler(String fileName){
}
 server.createContext("/tester", new testHandler("index.html"));