站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题利用Java实现Web服务器

利用Java实现Web服务器

摘要: HTTP协议的作用原理包括四个步骤
  一、HTTP协议的作用原理

  HTTP协议的工作原理包括四个步骤:

<iframe align=right marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></iframe>  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服务器连接。

<table borderColor=#ffcc66 cellSpacing=0 width="90%" align=center bgColor=#eefffe border=1> <tr> <td> //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="<head><BR>     Not Found</title></head><BR>     <body><hl>Error404-File notfound<BR>     </hl></body></html>";<BR>     outstream.println("HTTP /1.0 404 no found");<BR>     outstream.println("Content_Type:text /html");<BR>     outstream.println("Content_Length:" +notfound.length() +2);<BR>     outstream.println("");<BR>     outstream.println(notfound);<BR>     outstream.flush();<BR>    }<BR>   }<BR>   long m1=1;<BR>   while(m10)<BR>   {<BR>    if(s.substring(0,3).equalsIgnoreCase("GET"))return true;<BR>   }<BR>   return false;<BR>  }<BR>  /*获取要访问的文件名*/<BR>  String getfilename(String s){<BR>   String f=s.substring(s.indexOf('')+1);<BR>   f=f.substring(0,f.indexOf(''));<BR>   try{<BR>    if(f.charAt(0)=='/')<BR>     f=f.substring(1);<BR>    }catch(String IndexOutOfBoundsException e){<BR>     System.out.println("Exception:"+e);<BR>    }<BR>    if(f.equals(""))f="index.html";<BR>     return f;<BR>   }<BR>  /*把指定文件发送给Web浏览器*/<BR>  void sendfile(PrintStream outs,File file){<BR>   try{<BR>    DataInputStreamin=new DataInputStream(new FileInputStream(file));<BR>    int len=(int)file.length();<BR>    byte buf[]=new byte[len];<BR>    in.readFully(buf);<BR>    outs.write(buf,0,len);<BR>    outs.flush();<BR>    in.close();<BR>   }catch(Exception e){<BR>    System.out.println("Error retrieving file.");<BR>    System.exit(1);<BR>   }<BR>  }<BR> }</td></tr></TBODY></table><BR>  程序中的Connection Thread线程子类用来分析一个Web浏览器提交的请求,并将应答信息传回给Web浏览器。其中,getre quest()方法用来检测客户的请求是否为"GET";getfilename(s)方法是从客户请求信息s中获取要访问的HTML文件名;sendfile()方法把指定文件内容通过socket传回给Web浏览器。<BR><BR>zmbbs=1; </div> <hr class="hr1"/> <hr class="hr1"/> <br> <a href="/java/index.html">↑返回目录</a> <BR> 前一篇: <a href='/java/1/711.html'>轻松玩转Java配置的Classpath </a> <br> 后一篇: <a href='/java/1/713.html'>简析J2EE程序的数据库类设计模式 </a> </div> <!-- end: 正文 --> <!-- #EndEditable --> <!-- begin: footer --> <div id=footer><center> <a href="http://www.javanb.com/index.html">首页</a> | <a href="http://www.javanb.com/java-sitemap-bd.html">全站 Sitemap</a> | <a href="http://www.javanb.com/contact.html">联系我们</a> | <a href="#" onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.javanb.com/');return(false);">设为首页</a> | <a href="#" onclick="window.external.AddFavorite('http://www.javanb.com/', 'Java 编程资料牛鼻站');return false;">收藏本站</a> <br>版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved <br>   </center></div> <!-- end: footer --> </div> </body><!-- #EndTemplate --></html>