Vous êtes sur la page 1sur 5

this is the implementation of hyper text transfer protocol in java.

this program
can be use as sever which is able to process requsts for html pages.

to execute the program using localhost follow the steps:-


1.install httpserver.java on one system
2.configure your browser using following steps:
a.go to tools
b.go to internet options...
c.go to connections
d.go to lan setting
e.select use a proxy server for your lan
f.make address: http://localhost & port:100
3.run httpserver.java
4.run browser
5.put filename.html into the bin of jdk
5.place a request using following format:-
http://localhost.filename.html/
where filename.html is the name of the file you
want to request.

//httpserver.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class httpserver


{
public static void main(string [] args)
{
int i=1;

system.out.println("******************************************************
**************************");
system.out.println("****************************** http
server
***********************************");

system.out.println("******************************************************
**************************");
system.out.println("server started...");
system.out.println("waiting for connections...");
try
{

serversocket s = new serversocket(100);


for(;;)
{
socket incoming = s.accept();
system.out.println("new client connected with id "
+ i
+" from "+incoming.getinetaddress().gethostname() );
system.out.println("
");
system.out.println("
request header ");
thread t = new threadedserver(incoming,i);
i++;
t.start();
}
}
catch(exception e)
{
system.out.println("error: " + e);
}
}
}

class threadedserver extends thread


{
final static string crlf = "
";
socket incoming;
int counter;
public threadedserver(socket i,int c)
{
incoming=i;
counter=c;
}

public void run()


{
try
{
string statusline=null;
string contenttypeline=null;
string contentlength=null;
string venderline="server: executer 1.1";
string entitybody=null;
bufferedreader in =new bufferedreader(new
inputstreamreader(incoming.getinputstream()));
printwriter out = new
printwriter(incoming.getoutputstream(), true);
outputstream output=incoming.getoutputstream();
string headerline;
headerline=in.readline();
system.out.println(headerline);
/*string reqh;

boolean done=false;
while(!done)
{
reqh=in.readline();
if(reqh == null)
done = true;
else
{
//out.println("server>>> " + headerline);
system.out.println(reqh);
}
}

*/
stringtokenizer s = new stringtokenizer(headerline);
string meth = s.nexttoken();
if(meth.equals("get")||meth.equals("post"))
{
int dot1,dot2,fslash;
string fname,ext,filename;
string url = s.nexttoken();

dot1=url.indexof('.');
dot2=url.lastindexof('.');
fslash=url.lastindexof('/');
fname=url.substring(dot1+1,dot2);
ext=url.substring(dot2,fslash);
filename=fname+ext;
// system.out.println("fname:"+filename);
if(ext.equals(".html")||ext.equals(".htm"))
{
fileinputstream fis=null;
boolean filexists=true;
try
{
fis=new fileinputstream(filename);
}
catch(filenotfoundexception e)
{
system.out.println("exception: "+e.getmessage());
filexists=false;
}

if(filexists)
{
statusline=" http/1.1 200 ok"+crlf;
contenttypeline="content-type: text/html "+crlf;
contentlength="content-length:"+(new
integer(fis.available())).tostring() + crlf;
}
else
{

statusline = "http/1.0 404 not found" + crlf ;


contenttypeline = "content-type: text/html"+crlf ;
entitybody = "<html>" +
"<head><title>404 not found</title></head>" +
"<body><h1>404 file not found</h1></body></html>" ;
}

/* system.out.println("
responce header ");

system.out.println(statusline);
system.out.println(venderline);
system.out.println(contentlength);
system.out.println(contenttypeline);*/

output.write(statusline.getbytes());
output.write(venderline.getbytes());
output.write(contentlength.getbytes());
output.write(contenttypeline.getbytes());
output.write(crlf.getbytes());

if (filexists)
{
sendbytes(fis, output) ;
fis.close();
}
else
{
output.write(entitybody.getbytes());
}

}
else
{
statusline = "http/1.0 400 not found" + crlf ;
contenttypeline = "content-type: text/html"+crlf ;
entitybody = "<html>" +
"<head><title>400</title></head>" +
"<body><h1>400 a malformed http request is
reived</h1></body></html>";
}

else
{
statusline = "http/1.0 400 not found" + crlf ;
contenttypeline = "content-type: text/html"+crlf ;
entitybody = "<html>" +
"<head><title>400</title></head>" +
"<body><h1>400 a malformed http request is
reived</h1></body></html>";
}

boolean done=false;
while(!done)
{
headerline=in.readline();
if(headerline == null)
done = true;
else
{
system.out.println(headerline);
}
}

incoming.close();
in.close();
out.close();
}
catch(exception e)
{
system.out.println("error: " + e);
}
}
private static void sendbytes(fileinputstream fis, outputstream os)
throws exception
{
byte[] buffer = new byte[1024] ;
int bytes = 0 ;

while ((bytes = fis.read(buffer)) != -1 )


{
os.write(buffer, 0, bytes);
}
}

Vous aimerez peut-être aussi