This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How does web server locates the path" Watch "How does web server locates the path" New topic
Author

How does web server locates the path

Gaurav Chikara
Ranch Hand

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


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

Joined: Mar 22, 2000
Posts: 12761
    
    5

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

Joined: Jun 09, 2000
Posts: 410
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does web server locates the path
 
Similar Threads
FTP Socket Programming
Send a String to Server and server send it somewhere on network
how to exchange binary data between applet and server socket
Problem in WebServer
Wanted: Minimal Web Server Example