选择显示字体大小

用java实现web服务器

 

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

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

1.连接:web浏览器与web服务器建立连接。2.请求:web浏览器通过socketweb服务器提交请求。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.javajava编写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.get.netaddress().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浏览器。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons