一、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="<html><head><title>
Not Found</title></head>
<body><hl>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);
}
}
}
#p#分页标题#e#
措施中的Connection Thread线程子类用来阐明一个Web欣赏器提交的请求,并将应答信息传回给Web欣赏器。个中,getrequest()要领用来检测客户的请求是否为"GET";getfilename(s)要领是从客户请求信息s中获取要会见的HTML文件名;sendfile()要领把指定文件内容通过socket传回给Web欣赏器。