服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用java实现web服务器

用java实现web服务器 
 
一、http协议的作用原理

  http协议的作用原理包括四个步骤:

1.连接:web浏览器与web服务器建立连接。2.请求:web浏览器通过socket向web服务器提交请求。3.应答:web浏览器提交请求后,通过http传送给web服务器。web服务器接到请求后,进行事务处理,处理结果又通过http传回给web浏览器,从而在web浏览器上显示出所请求的页面。4.关系连接:当应答结束后,web浏览器与web服务器必须断开,以保证其它web浏览器能够与web服务器建立连接。

二、用java实现web服务器的程序设计

  根据上述http协议的作用原理,实现get请求的web服务器程序的方法如下:

1.创建serversocket类对象,监听端口8080。这是为了区别于http的标准tcp/ip端口80而取的;2.等待、接受客户机连接到端口8080,得到与客户机连接的socket;3.创建与socket关联的输入流instream和输入出流outstream;
式为:get路径/文件名http/1.0;4.从与socket关联的输入流instream中读取一行客户机提交的请求信息,请求信息的格式为:get路径/文件名http/1.0;5.从请求信息中获取请求类型。如果请求类型是get,则从请求信息中获取所访问的html文件名。没有html文件名时,则以index.htm1作为文件名;6.如果html文件存在,则打开html文件,把http头信息和html文件内容通过socket传回给web服务器,然后关闭文件,否则发送错误信息给web浏览器;7.关闭与相应web浏览器连接的socket字。

  下面的程序是根据上述方法编写的,可实现多线程的web服务器,以保证多个客户机能同时与该web服务器连接。

 //webserver.java用java编写web服务器

 import java.io.*;

 import java.net.*;

 import java.util.date;

 public class webserver{

 public static void main(string args[])

{

 int i=1,port=8080;

 serversocket server=null;

 socketclient=null;

 try{

 server=new serversocket(port);

 system.out.println

("web server is listening on port"

+server.getlocalport());

 for(;;){

 client=server.accept();

//接受客户机的连接请求

 new connection thread(client,i).start();

 i++;

 }

 }catch(exception e){system.out.println(e);}

 }

 }

/*connnection thread类完成

与一个web浏览器的通信*/

 class connection thread extends thread{

 socket client;//连接web浏览器的socket字

 int counter;//计数器

 public connection thread(socketcl,int c){

 client=cl;

 counter=c;

 }

 public void run()//线程体

 {

 try{

 string deskip=client.getinetaddress().tostring();

//客户机ip地址

 int destport=client.getport();

//客户机端口号

 system.out.println

("connecction"+counter+":

connected to "+destip+"on port

 "+destport+".");

 printstream outstream=new printstream

(client.getooutputstream());

 datainputstreaminstream+new datainputstream

(client.getinputstream());

 string inline=instream.readline();

//读取web浏览器提交的请求信息

 system.out.println("received:"+inline);

 if(getrequest(inline)){//如果是get请求

 string filename=getfilename(inline);

 file file=new file (filename);

 if(file.exists()){

//若文件存在,则将文件送给web浏览器

 system.out.println(filename+"requested.");

 outstream.println("http/1.0200ok");

 outstream.println("mime_version:1.0");

 outstream.println("content_type:text/htm1");

 int len=(int)file.length();

 outstream.println("content_length:"+len);

 outstream.println("");

 sendfile(outstream,file);//发送文件

 outstream.flush();

 }else{//文件不存在时

 string notfound="htmlheadtitle

not found/title/head

 bodyhlerror404-file notfound

/hl/body/html";

 outstream.println("http /1.0 404 no found");

 outstream.println("content_type:text /html");

 outstream.println

("content_length:" +notfound.length() +2);

 outstream.println("");

 outstream.println(notfound);

 outstream.flush();

 }

 }

 long m1=1;

 while(m10)

 {

 if(s.substring(0,3).equalsignorecase

("get"))return true;

 }

 return false;

 }

 /*获取要访问的文件名*/

 string getfilename(string s){

 string f=s.substring(s.indexof(/)+1);

 f=f.substring(0,f.indexof(/));

 try{

 if(f.charat(0)==/)

 f=f.substring(1);

 }catch(string indexoutofboundsexception e){

 system.out.println("exception:"+e);

 }

 if(f.equals(""))f="index.html";

 return f;

 }

 /*把指定文件发送给web浏览器*/

 void sendfile(printstream outs,file file){

 try{

 datainputstreamin=new datainputstream

(new fileinputstream(file));

 int len=(int)file.length();

 byte buf[]=new byte[len];

 in.readfully(buf);

 outs.write(buf,0,len);

 outs.flush();

 in.close();

 }catch(exception e){

 system.out.println("error retrieving file.");

 system.exit(1);

 }

 }

 }

  程序中的connection thread线程子类用来分析一个web浏览器提交的请求,并将应答信息传回给web浏览器。其中,getrequest()方法用来检测客户的请求是否为"get";getfilename(s)方法是从客户请求信息s中获取要访问的html文件名;sendfile()方法把指定文件内容通过socket传回给web浏览器。

扫描关注微信公众号