0

I use NanoHTTP to set up a web server with HTML, JS and some Java. Now I want to get the JavaScript resource out of the HTML by using

private String readFileAsTextUsingInputStream(String filename) {
   if(Objects.equals(filename, "/login")) {
        filename = "/Users/myName/Desktop/Arbeit/Lehrerkalender/src/main/resources/html/login.html";
    }
   return Connection.class.getResourceAsStream(filename).readAllBytes().toString();
}

inside my Connection.java. This function is called in the "serve" method. The serve method (I think) is being called, when you open the website or change the session uri. This method looks like this:

    @Override
    public Response serve(IHTTPSession session) {
        String msg = null;

        if (Objects.equals(session.getUri(), "/login")) {

            try {
                msg = readFileAsTextUsingInputStream(session.getUri());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return newFixedLengthResponse(msg);

        } else if(session.getUri().isBlank() || Objects.equals(session.getUri(), "/")) {
            msg = "<html><body><h1>Die Website finden Sie<a href=http://"+this.getHostname()+":"+
                    this.getListeningPort()+"/login>hier</a></h1></body></html>".trim();
        }

        return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, getMimeType(session.getUri()), msg);
    }

So, when I start the program and go to "http://localhost:9080/login" the program tries to identify the HTML and JS but already fails at the HTML. I've also tried it with giving the absolute path and path from the root and from the class where this is being called from. I also tried using ClassLoader and everything but it always tells me that the return value of "java.lang.Class.getResourceAsStream(String)" is null.

The project is build like this:

  • Lehrerkalender
    • src
      • main
        • java
          • connection (this is a folder)
            • Connection.java
        • resources
          • html (this is a folder)
            • login.html
          • js (this is a folder)
            • loginhandler.js

Thanks already for your help!

Erwin109
  • 17
  • 6
  • I does not work with the absolute path. I‘ve tried it already. And "/Users/myName/Desktop/Arbeit/Lehrerkalender/src/main/resources/html/login.html" is the absolute path – Erwin109 Nov 24 '21 at 18:33
  • Well now it doesn't return null anymore but when I load the website there is just [B@54ba93f3 written on it. Is it because of the charset maybe? – Erwin109 Nov 24 '21 at 18:43
  • But where is an array being parsed? – Erwin109 Nov 24 '21 at 18:47
  • Ok, thank you. Do you have any clue where I have to call it? I am sorry I am pretty new to Java and this project is very important for me. It's for my education school. – Erwin109 Nov 24 '21 at 18:54

0 Answers0