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
Greenhorn
Joined: May 02, 2002
Posts: 15
posted
0
Gaurav Chikara
Ranch Hand
Joined: Jun 09, 2000
Posts: 410
posted
0
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 http://localhost : 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 ); while(true){ socket = serverSocket.accept(); System.out.println("accepted"); 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(); System.out.println("br.readline()"+requestLine); StringTokenizer stringTokenizer = new StringTokenizer(requestLine); String method = stringTokenizer.nextToken(); String file = stringTokenizer.nextToken(); file = "."+file; System.out.println("file"+file); // PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream())); System.out.println("hello doctor"); try { FileInputStream fin = new FileInputStream(file); System.out.println("reached here"); } catch(FileNotFoundException ex) { System.out.println("Not Found"); } }//outer while }//main } [ September 20, 2002: Message edited by: Gaurav Chikara ]
SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door