The moose likes Java in General and the fly likes Problem in WebServer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in WebServer " Watch "Problem in WebServer " New topic

Problem in WebServer

Abhimanyu dagla

Joined: Jan 11, 2001
Posts: 20

Hello friends,
I have a problem with this Code written for a server.It caters to the requests made by the browser .
But the HTml page never loads .the browser says it is opening the
default page..the progess bar comes to 50% and halts.
Please help.(OF course, get a HTML file as default file)
// a simple webserver
import java.io.*;
import java.util.*;
import java.net.*;
class Configuration {
int port;
String defaultfile;
defaultfile= "index.html"; // please get a html in your folder.
public int getPort(){
return port;
public String getDefaultFile(){
return defaultfile;
public void displayConfiguration(){
System.out.println("port: "+port);
System.out.println("defaultfile: "+defaultfile);
public class WebServer{
Configuration config;
config=new Configuration();

void startServer(){
ServerSocket server =new ServerSocket(config.getPort());
System.out.println("server started");
Socket c= server.accept();
new WebServerThread(c,config);// creating thread
catch(Exception e){ e.printStackTrace(); }
public static void main(String args[]){
WebServer ws= new WebServer();
class WebServerThread extends Thread{
DataInputStream din;
FilterOutputStream out;
Configuration config;
Socket client;
WebServerThread(Socket c, Configuration con){
client =c;
System.out.println("server thread started");
public void run(){
din= new DataInputStream(new BufferedInputStream(client.getInputStream()));
out= new FilterOutputStream(new BufferedOutputStream(client.getOutputStream()));
Vector lines= new Vector();
String l= din.readLine();
if(l.length()<1) break;<br /> lines.addElement(l);<br /> System.out.println(l);<br /> }<br /> while(true);<br /> System.out.println("reading the lines");<br /> String firstline =(String)lines.elementAt(0);<br /> if(firstline.startsWith("GET"))<br /> processGetRequest(firstline);<br /> else<br /> if(firstline.startsWith("POST"));<br /> processPostRequest(firstline);<br /> System.out.println("Request fulfilled: breaking connection");<br /> client.close();<br /> din.close();<br /> out.close();<br /> }<br /> catch (Exception e){ e.printStackTrace(); }<br /> }<br /> public void processPostRequest(String firstline){<br /> try{<br /> String data=din.readLine();<br /> System.out.println(data);<br /> } catch(Exception e){ e.printStackTrace(); }<br /> }<br /> <br /> public void processGetRequest(String firstline){<br /> System.out.println("about to process the GET request");<br /> String filename= getFileName(firstline);<br /> File file= new File(filename);<br /> if(file.exists())<br /> {<br /> sendFile(file);<br /> System.out.println("sent the file");<br /> }<br /> else<br /> System.out.println("File not found");<br /> }<br /> <br /> public String getFileName(String firstline){<br /> String filename =null;<br /> StringTokenizer st= new StringTokenizer(firstline);<br /> String temp= st.nextToken();<br /> filename=st.nextToken();<br /> temp= st.nextToken();<br /> if(filename.length()>1)
return filename;
public void sendFile(File file){
FileInputStream fin= new FileInputStream(file);
DataInputStream in= new DataInputStream(fin);
byte buff[]= new byte[(int)file.length()];
for(int i=0; i<buff.length; i++){>
System.out.println("File "+file.getName()+" sent successfully");
catch(Exception e){ e.printStackTrace(); }

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Abhimanyu,
I'm going to move this to the Java intermediate forum as it's not related to Certification study.
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Your response to the client has to start with an HTTP response line, like one of these:
HTTP/1.1 200 OK
HTTP/1.1 404 Not found
then any HTTP headers:
Content-type: text/html
Content-length: whatever
followed by one blank line, and then the data.
If you're writing a web server (which is really not difficult, as you have discovered), you really should refer to the HTTP specification, RFC2616 at
http://www.ietf.org/rfc/rfc2616.txt . It has all the answers.

Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Abhimanyu dagla

Joined: Jan 11, 2001
Posts: 20

Thanks for the link , phil.
The Server code has run, 'finally'.
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem in WebServer
Similar Threads
Problem using RTFEditorKit
C --> Java
How does web server locates the path
sending a file to a client over a server
Wanted: Minimal Web Server Example