• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wanted: Minimal Web Server Example

Siegfried Heintze
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone have a minimal java program that will pump static HTML files out on a socket when a browser connects with an HTTP GET or POST?
I expect it to be less than 100 lines. I could write it myself but surely someone has already done it many times.
Donal Lynch
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Donal
I am also working on web server
I need your guidance
Since I make my web server program run
then in which directory shall i have to keep my html files so that
: port/index.html shows a browser page
I am facing the problem that even though I have placed default.html in same directory in which my class file is still I am getting FileNotFoundException
My server code is like this
package webserver;
import java.io.*;
import java.net.*;
import java.util.*;
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
public class FileServer {
static int port = 34;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
Socket socket;
System.out.println( "Starting server on port " + port );
socket = serverSocket.accept();
HttpRequest httpRequest = new HttpRequest(socket);
// Thread thread = new Thread(httpRequest);
// thread.start();
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
DataInputStream din = new DataInputStream(new BufferedInputStream(in));
ArrayList arList = new ArrayList();
String requestLine = br.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(requestLine);
String method = stringTokenizer.nextToken();
String file = stringTokenizer.nextToken();
file = "."+file;
// PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println("hello doctor");
FileInputStream fin = new FileInputStream(file);
System.out.println("reached here");
catch(FileNotFoundException ex) {
System.out.println("Not Found");
}//outer while
[ September 20, 2002: Message edited by: Gaurav Chikara ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic