📄 用java实现web服务器.htm
字号:
<BR> System.out.println("Web Server is listening on
port "+server.getLocalPort());<BR> for (;;)
{<BR> client=server.accept(); //
接受客户机的连接请求<BR> new
ConnectionThread(client,i).start();
<BR> i++;<BR> }<BR> } catch (Exception e)
{System.out.println(e);}<BR> }<BR> }</P>
<P> /* ConnnectionThread类完成与一个Web浏览器的通信 */<BR> class
ConnectionThread extends Thread {<BR> Socket client;
// 连接Web浏览器的socket字<BR> int counter; //
计数器<BR> public ConnectionThread(Socket cl,int c)
{<BR> client=cl;<BR> counter=c;<BR> }<BR> public
void run() // 线程体<BR> {<BR> try {<BR> String
destIP=client.getInetAddress().toString(); //
客户机IP地址<BR> int destport=client.getPort(); //
客户机端口号<BR> System.out.println("Connection
"+counter+":connected to "+destIP+" on port
"+destport+".");<BR> PrintStream outstream=new
PrintStream(client.getOutputStream());<BR> DataInputStream
instream=new
DataInputStream(client.getInputStream());<BR> String
inline=instream.readLine(); //
读取Web浏览器提交的请求信息<BR> System.out.println("Received:"+inline);<BR> if
(getrequest(inline)) { // 如果是GET请求<BR> String
filename=getfilename(inline);<BR> File file=new
File(filename);<BR> if (file.exists()) { //
若文件存在,则将文件送给Web浏览器<BR> System.out.println(filename+"
requested.");<BR> outstream.println("HTTP/1.0 200
OK");<BR> outstream.println("MIME_version:1.0");<BR> outstream.println("Content_Type:text/html");<BR> int
len=(int)file.length();<BR> outstream.println("Content_Length:"+len);<BR> outstream.println("");<BR> sendfile(outstream,file);
// 发送文件<BR> outstream.flush();<BR> } else {
// 文件不存在时<BR> String
notfound="<html><head><title>Not
Found</title></head><BR> <body><h1>Error
404-file not
found</h1></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 (m1<11100000) {m1++;} //
延时<BR> client.close();<BR> } catch (IOException
e)
{<BR> System.out.println("Exception:"+e);<BR> }<BR> }</P>
<P> /* 获取请求类型是否为“GET” */<BR> boolean
getrequest(String s) { <BR> if
(s.length()>0)<BR> {<BR> if
(s.substring(0,3).equalsIgnoreCase("GET")) return
true;<BR> }<BR> return false;<BR> }</P>
<P> /* 获取要访问的文件名 */<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 (StringIndexOutOfBoundsException e)
{<BR> System.out.println("Exception:"+e);<BR> }<BR> if
(f.equals("")) f="index.html";<BR> return
f;<BR> }</P>
<P> /*把指定文件发送给Web浏览器 */ <BR> void
sendfile(PrintStream outs,File file) {<BR> try
{<BR> DataInputStream in=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> }</P>
<P> 程序中的ConnectionThread线程子类用来分析一个Web浏览器提交的请求,并将应答信息传回给Web浏览器。其中,getrequest()方法用来检测客户的请求是否为“GET”;getfilename(s)方法是从客户请求信息s中获取要访问的HTML文件名;sendfile()方法把指定文件内容通过socket传回给Web浏览器。<BR> 对上述程序的getrequest()方法和相关部分作修改,也能对POST请求进行处理。</P>
<P> <STRONG>三、运行实例</STRONG></P>
<DIV> 为了测试上述程序的正确性,将编译后的WebServer.class、ConnectionThread.class和下面的index.html文件置于网络的某台主机的同一目录中(如:主机NT40SRV的C:\JWEB目录)。<BR>程序2:index.html文件<BR> <HTML><BR> <HEAD><BR> <META
HTTP-EQUIV="Content-Type" content="text/html;
charset=gb_2312-80"><BR> <TITLE>Java
Web服务器</TITLE><BR> </HEAD><BR> <BODY><BR> <h3>这是用JAVA写出的WEB服务器主页</h3><BR> 1998年8月28日<BR> <hr><BR> </BODY><BR> </HTML><BR> 首先在该主机上用java命令运行WebServer.class:<BR> C:\jweb>java
webserver<BR> 然后在客户机运行浏览器软件,在URL处输入WebServer程序所属的URL地址(如:<A
href="http://nt40srv:8080/index.html">http://nt40srv:8080/index.html</A>),就在浏览器窗口显示出指定的HTML文档。<BR> 注意,不能缺省端口号8080,如缺省,则运行该主机的正常WEB服务器。<BR> 说明,不具备网络条件的可在安装了Windows
95的单机上进行测试,方法是用localhost或127.0.0.1代替URL地址的域名部分,即URL地址为<A
href="http://localhost:8080/">http://localhost:8080/</A>。</DIV></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width=10 height=11><IMG height=11
src="用Java实现Web服务器.files/u_16.gif" width=10></TD>
<TD style="BORDER-BOTTOM: #e8e8e8 1px solid" width=695
bgColor=#f9f9f9 height=11><IMG height=1 src="" width=1></TD>
<TD width=10 height=11><IMG height=11
src="用Java实现Web服务器.files/u_17.gif"
width=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<DIV align=right>页面功能 【<A
href="javascript:window.external.AddFavorite(location.href,document.title+'--www.JavaFan.NET');">加入收藏</A>】
【<A
onclick="window.open(this.href,'','top=180,left=240,width=342,height=326,scrollbars=yes,resizable=no');return false;"
href="http://www.javafan.net/sendarticle.jsp?title=用Java实现Web服务器&URL=20040313181239641">推荐给朋友</A>】
【字体:<A class=black href="javascript:fontZoom(15)">大</A> <A
class=black href="javascript:fontZoom(13)">中</A> <A class=black
href="javascript:fontZoom(12)">小</A>】 【<A class=black
href="javascript:window.close()">关闭</A>】 </DIV></TD></TR>
<TR>
<TD vAlign=top><IMG height=12 src="用Java实现Web服务器.files/u_06.gif"
width=751></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=750 align=center border=0>
<TBODY>
<TR>
<TD bgColor=#cacaca height=1></TD></TR>
<TR>
<TD vAlign=center align=middle height=30>Copyright © 2003 - 2005
JavaFan.NET All Rights Reserved</TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -