Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How does web server locates the path  RSS feed

Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to craete a web server using TCP Sockets but I am not been able to figure out as how the web server locates the requested file name and where it locates it
Here is my sample code
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 fileName = stringTokenizer.nextToken();
// file = "."+file;
String parentName = System.getProperty("user.dir");
File file = new File(fileName);
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");
out.print( "HTTP/1.0 404 Not Found\r\n" );
out.print( "Content-length: 9\r\n" );
out.print( "\r\n" );
out.print( "not found" );
}//outer while
where my FileServer class is in directory
the requested file "default.html" is in
even though i try tp put it in the directory
the program is still not able to locate the file
can any one tell how to rectify this situation
Thankx in advance
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It appears that you are not using a path when creating file - thus it would only work if the fileName was in the "current" directory.
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hI WILLIAM Thnkx for reply
But my question was
how server decides that all files in a directory (say home directory) will be considered by it as its root directory
eg. http://servername/File1.html
so i want to know in which directory server will prefer File1.html directory to be accessible by it
What's that smell? Hey, sniff this tiny ad:
Why should you try IntelliJ IDEA ?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!