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 6789607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:37:28+00:00 2026-05-26T17:37:28+00:00

I’m trying to implement a basic web server in Java. When I direct a

  • 0

I’m trying to implement a basic web server in Java. When I direct a web browser to 127.0.0.1:8020/webpage.html, the server receives the request header, but when it tries to send the webpage back it doesn’t show up on the browser.

Can anyone help?

public class WebServer
{
    public static void main(String[] args)
    {

        try
        {
            ExecutorService scheduler = Executors.newCachedThreadPool();
            ServerSocket server = new ServerSocket(8020);
            while (true)
            {
                Socket client = server.accept();
                Runnable r = new HTTPThread(client.getInputStream(), client.getOutputStream());
                scheduler.execute(r);      
                System.out.println("LOG: New Thread Created");
            }
        }
        catch (IOException e)
        {
            System.out.println("ERROR: Cannot listen on socket");
            e.printStackTrace(); 
        }
    }
}


public class HTTPThread implements Runnable {
    InputStream in;
    OutputStream out;

    private String fileName;
    private String fileLoc;
    private static final String rootLoc = "C:\\Users\\myName\\workspace\\HTTPServer\\src\\";

    HTTPThread(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    public void run() {
        try {

            // show request headers and store http header
            System.out.println("LOG: Preparing to read request");
            String header = readInput(in);
            System.out.println("LOG: Request read successfully!");
            System.out.println(header);


            // get file name from request header
            fileLoc = rootLoc + header.substring(5, header.indexOf("H") - 1);
            System.out.println(fileLoc);


            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));

            //send response header and body
            writer.print("HTTP/1.0 200 OK\nContent-Type: text/html\n\n" + fileReader(fileLoc));
            System.out.println("LOG: Response Sent");

            out.close();
            in.close();         
        }

        catch (IOException e) {
            System.out.println("ERROR: Could not listen on socket");
            System.exit(-1);
        }

    }

    //Returns content of webpage as a string (html)
    public String fileReader(String file) {

        StringBuilder text = new StringBuilder();
        String NL = System.getProperty("line.separator");
        Scanner scanner;
        try {
            scanner = new Scanner(new FileInputStream(file));
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine() + NL);
            }

            return text.toString();
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: File Not Found");
            System.exit(-1);
            return null;
        }
    }

    //Reads bytestream from client and returns a string
    private String readInput(InputStream is) {

        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        try 
        {
            String current;
            String header = "";
            while (!(current = in.readLine()).isEmpty())
            {
                header += current + System.getProperty("line.separator");       
            }

            return header;
        }
        catch (IOException e) 
        {
            e.printStackTrace();
            return null;
        }
    }
}
  • 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-26T17:37:29+00:00Added an answer on May 26, 2026 at 5:37 pm

    Adding writer.close(); before out.close(); would fix it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.