首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
yjz0065 (115)
iiprogram (70)
ShowLong (2)
coofucoo (106)
psyl (153)
capsicum29 (8)
qdzx2008 (51)
cyp403 (16)
lphpc (31)
smallnest (63)
CSDN - 文档中心 - Java 阅读:2428   评论: 6    参与评论
标题   用Java实现Web服务器 HTTP协议     选择自 hem 的 Blog
关键字   用Java实现Web服务器 HTTP协议
出处   http://www.victory-tec.com/chinese/jslt/java/

用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="<html><head><title>

Not Found</title></head>

 <body><hl>Error404-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浏览器。



相关文章
对该文的评论
liyong33 ( 2004-07-05)
怎么贴出来的代码一堆的错误呀???
dream_girl ( 2001-06-10)
zjxyz:我连不上你的站点,我现在急需文档和程序。
      很不好意思,今年我为了考GRE,耽误了自己的专业课,没有时间开发程序了,现在我们
      老师正催着要呢。你能帮帮我吗?
dream_girl ( 2001-06-10)
zjxyz:你的程序有没有设计文档?
      如果有的话,能不能把文档及源代码寄一份给我
      谢谢
      dream_cherry@163.net
hem ( 2001-06-07)
zjxyz  
  有点慢, 连不上。
windygirl ( 2001-06-07)
基本的东西都有了,可以