2023信创独角兽企业100强
全世界各行各业联合起来,internet一定要实现!

用Java实现Web服务器

2004-02-15 eNet&Ciweek

  一、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=" <br/>Not Found</title></head> <br/> 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浏览器。


相关频道: eNews

您对本文或本站有任何意见,请在下方提交,谢谢!

投稿信箱:tougao@enet16.com