0

I've read a request for an HTML document from my browser, parsed the file from the request, found the specified file, and now all that's left is to send back the contents of the HTML file to the browser. What I'm currently doing seems like it should work just fine, however, the contents of the HTML file are not received by the browser.

public void sendResponse(File resource){
        System.out.println(resource.getAbsolutePath());

        Scanner fileReader;
        try {
            fileReader = new Scanner(resource);

            while(fileReader.hasNext()){
                socketWriter.println(fileReader.nextLine());
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            e.printStackTrace();
        }
    }

What am I doing incorrectly? There is no exception thrown, but the browser just keeps loading and loading.

3
  • the scenario is unclear. What is your program an applet, a servlet, standalone? Commented Oct 19, 2011 at 21:48
  • are you flushing/closing the socket? Commented Oct 19, 2011 at 21:49
  • Plus the closing/etc., are you returning proper response headers? Commented Oct 19, 2011 at 21:53

3 Answers 3

1

that suggests your code is stuck in an infinite loop. Check your while loop. nextLine() is not moving the file pointer ahead?

Sign up to request clarification or add additional context in comments.

Comments

0

It's hard to tell without knowing what type socketWriter is, but I imagine you'll need to close the connection. Look for a close() method or something similar on socketWriter and call it when you're done.

Comments

0

It is not evident from your code, where socketWriter is going. Low level operations such as socket are best handled by the web server itself. Normally when we have to write a response back to the browser, we make use of HttpServletResponse object which is available in the goGet / doPost method of your servlet. Refer to the javadocs for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.