荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: hayas.bbs@bbs.gliet.edu.cn (阿生), 信区: Linux
标  题: 用Java实现Web服务器
发信站: 漓江夜话 (Thu Nov 21 21:39:37 2002)
转信站: SZU!news.tiaozhan.com!LiRiver


  一、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.创建与sock
et关联的输入流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";getf
ilename(s)方法是从客户请求信息s中获取要访问的HTML文件名;sendfile()方法把指定文
件内容通过socket传回给Web浏览器。





--

※ 来源:.漓江夜话  WWW http://bbs.gliet.edu.cn  [FROM: 192.168.135.131]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店