Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wanted: Minimal Web Server Example  RSS feed

 
Siegfried Heintze
Ranch Hand
Posts: 417
  • 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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • 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
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!