![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| 用java实现web服务器 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-7-1 ![]() |
||||||
|
用Java实现Web服务器 HTTP协议的作用原理包括四个步骤: 1.连接:Web浏览器与Web服务器建立连接。
二、用Java实现Web服务器的程序设计 根据上述HTTP协议的作用原理,实现GET请求的Web服务器程序的方法如下: 1.创建ServerSocket类对象,监听端口8080。这是为了区别于HTTP的标准TCP/IP端口80而取的;2.等待、接受客户机连接到端口8080,得到与客户机连接的socket;3.创建与socket关联的输入流instream和输入出流outstream; 下面的程序是根据上述方法编写的,可实现多线程的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";getfilename(s)方法是从客户请求信息s中获取要访问的HTML文件名;sendfile()方法把指定文件内容通过socket传回给Web浏览器。
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|