Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:02:11+00:00 2026-05-26T10:02:11+00:00

I have a simple HTML server program that listens on port 8000 for a

  • 0

I have a simple HTML server program that listens on port 8000 for a request. I want to be able to request an HTML file from the server program and send the requested HTML back to the browser. Currently the server receives the request and parses the requested filename from the request, and will even print the contents of the html file to the console. However, when I try to print the HTML file contents to the connection’s socket (back to the browser) nothing happens; the browser just continues loading.

My server class is relatively simple and just creates instances of this runnable class, RequestHandler, for each connection:

package server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class RequestHandler extends Thread {

    Object block;
    ServerSocket serverSocket;
    BufferedReader socketReader;
    PrintWriter socketWriter;

    public RequestHandler(Object block, ServerSocket serverSocket){
        this.block = block;
        this.serverSocket = serverSocket;
    }

    @Override
    public void run() {
        try{
            System.out.println("Waiting for request...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Connection made.");

            synchronized(block){
                System.out.print("Notifying server thread...");
                block.notify();
                System.out.println("...done");
                System.out.println();
            }

            socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));            
            socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);

            String input;
            while((input = socketReader.readLine()) != null){
//              System.out.println(input);

                if(input.startsWith("GET")){
                    getResource(input);
                }
            }
        }catch(IOException e){
            System.out.println("IOException!");
            e.printStackTrace();
        }
    }

    public void getResource(String getRequest){
        String[] parts = getRequest.split("\\s+");
        String filename = parts[1].substring(1);

        System.out.println(filename);
        File resource = new File(filename);             
        sendResponse(resource);
    }

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

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

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:02:12+00:00Added an answer on May 26, 2026 at 10:02 am

    What should you always do when you’re done writing to or reading from a stream? (I know the answer to your question, but I’m asking this question because you labeled your question as “homework”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple HTML server that will read a request from
I have a simple HTML file which references .gadget so that it can be
I have a simple HTML (as HTA) application that shows strange behavior on Windows
I'm trying to have a simple html table, that highlights a row as a
can anyone help? I have a simple html file which i am filling via
Hi I have a simple browser that sends a request to yahoo.com which the
I want to create a very simple HTML/AJAX based GUI for a Python program.
Okay so I have a simple HTML Input fragment: <input id=txtFirstName type=text class=txtBox runat=server
I have a simple Spring MVC application that looks up some user details from
We have a legacy application running tomcat that publishes data in a simple html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.