• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does web server locates the path

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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 );
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 fileName = stringTokenizer.nextToken();
// file = "."+file;
System.out.println("file"+fileName);
String parentName = System.getProperty("user.dir");
File file = new File(fileName);
System.out.println("file.exists"+file.exists());
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");
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
}//main
}
where my FileServer class is in directory
E:\jbproject\WebServer\classes
and
the requested file "default.html" is in
E:\jbproject\WebServer
even though i try tp put it in the directory
E:\jbproject\WebServer\classes
the program is still not able to locate the file
can any one tell how to rectify this situation
Thankx in advance
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • 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.
Bill
 
Gaurav Chikara
Ranch Hand
Posts: 412
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic