Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problem in WebServer

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
Configuration(){
port=80;
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;
WebServer(){
config=new Configuration();
}

void startServer(){
try{
ServerSocket server =new ServerSocket(config.getPort());
System.out.println("server started");
config.displayConfiguration();
while(true){
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();
ws.startServer();
}
}
class WebServerThread extends Thread{
DataInputStream din;
FilterOutputStream out;
Configuration config;
Socket client;
WebServerThread(Socket c, Configuration con){
client =c;
config=con;
this.start();
System.out.println("server thread started");
}
public void run(){
try{
din= new DataInputStream(new BufferedInputStream(client.getInputStream()));
out= new FilterOutputStream(new BufferedOutputStream(client.getOutputStream()));
Vector lines= new Vector();
do{
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)
{
filename.substring(1);
if(filename.endsWith("/"))
filename+=config.getDefaultFile();
}
else
{
filename=config.getDefaultFile();
}
return filename;
}
public void sendFile(File file){
try{
FileInputStream fin= new FileInputStream(file);
DataInputStream in= new DataInputStream(fin);
byte buff[]= new byte[(int)file.length()];
in.close();
for(int i=0; i<buff.length; i++){>
out.write(buff[i]);
out.flush();
}
System.out.println("File "+file.getName()+" sent successfully");
}
catch(Exception e){ e.printStackTrace(); }
}
}

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Abhimanyu dagla
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the link , phil.
The Server code has run, 'finally'.
Abhimanyu
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic