This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes How does web server locates the path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How does web server locates the path" Watch "How does web server locates the path" New topic

How does web server locates the path

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 411
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

SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037

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

Joined: Jun 09, 2000
Posts: 411
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
I agree. Here's the link:
subject: How does web server locates the path
jQuery in Action, 3rd edition