问答题 HTTP 协议
HTTP请 求消息示例
GET/index, htmIHTTP/1.1
Accept: image/gif, image/jpeg, */*
Accept-Language: zh-ch
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE6.0; Windows NT5.1)
Host: IocaIhost: 8080
Connection: Keep-Alive
HTTP/1.1 200 OK
Servert: Microsoft-IIS/4.0
Date: Mon, 3 Jan 1998 13:13:33 GMT
Content-Type: text/html
Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT
Contelit-Length: 112
# < html >


2.相关类及主要成员函数
ServerSocket类:
服务器端套接字,它监听固定端口,以接收来自客户端的连接请求,一旦建立连接就返回一个Socket类型的对象。类中的主要成员函数见表1。

表1 类中的主要成员函数

函数名
功能说明
ScrvcrSocket(PORT)
构造函数
accept( )
返回建立连接的Sockct类型的对象
close( )
关闭当前的SerrorSocket类型的对象
基于连接的套接字。类中的主要成员函数见表2。

表2 类中的主要成员函数

函数名
功能说明
getnetAddress( )
返回InetAddress类型的对象,该对象封装了与该套接字所关联的客户机的IP地址
gctInputSffeam( )
返回当前对象所关联的输入流
gctouputStream( )
返回当前对象所关联的输出流
close( )
关闭当前对象
{{B}}[Java源程序;一个简单的web服务器]{{/B}}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*WebScrvc. java */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
packageobjclass;
importjaVa. net. *;
importjaVa. io. *;
public class WebServer{
//web服务器侦听的端口号
public static final int PORT=8080;
//WEB-ROOT变量存放web服务器工作目录,该目录存放HTML、GIF图片等静态文件资源
public static final String WEB-ROOT=
Systern. getProperty("user.dir")+File. separator+"webmot";
//SHUTDOWN_COMMAND变量保存关闭服务器的命令
private static final string SHUTDOWN_COMMAND="/shutdown";
//是否收到关闭服务器命令的标志
private boolean Shutdown=false;
public static void main(Sting[]args){
WebServerserver; newWebserver( );
Server.await( );
}
public void await( ){
ServerSocke serverSocke=null;
try{
//创建侦听端口号为PORT的ServerSocket类型的对象
ServerSocket=new(1);
System. out. println("WebServerStarted!");
!
catch(IOException e){
e.printStackTrace( );
System.exit{{U}} (1) {{/U}};
}
//循环等待客户端连接
While(!Shutdown){
Socket socket=null;
InputStream input=null;
OutputStream output=null;
try{
//创建来自客户端的连接套接宇
Socket={{U}} (2) {{/U}};
//创建输入流
input=socket. {{U}}(3) {{/U}};
//创建输出流
Output=socket.{{U}} (4) {{/U}};
//创建request对象
Request # request=new Request(input);
//解析客户端HTTP请求
request, parse( );
//创建Response 对象
Response response=new {{U}}(5) {{/U}};
//将 request 对象传递给 response 对象
response. setRequest(request);
//给客户端发送所请求的静态资源
response. {{U}}(6) {{/U}};
//关闭连接套接字
{{U}} (7) {{/U}};
//判断当前HTTP 请求中的URI是否是关闭服务器命令
shutdown-request. getUri( ). equals(SHUTDOWN_COMMAND);
Catch (Exception e) {
e. {{U}}(8) {{/U}};
continue;
}
}
}
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * Request. java * /
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
package objclass;
import java.io.InputStream;
import java.io.IOEexception;
public Class Request{
private InputStream input;
private String uri;
public Request(InputStream input) {this. input=input;}
//解析客户端HTTP请求信息
public void parse( )[…parseUrI( );…]
//解析客户端HTTP请求中的URL
private String parseUrl(String requestString){…}
public String getUrl( ){return uri;}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ *Response.java * /
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
package objclass;
import java. io. *;
public class Response {
private static final int BUFFER_SIZE=1024;
Request request;
Output Stream output ;
public Response( OutputStrearn output) { this. output=output; }
public void setRequest(Requestrequest){this. request=request;}
//向客户端发送所请求的静态资源
public void sendStaticResource( ) throwsIOException}…}
}
[运行测试]
在本机运行WebServer程序,然后打开IE浏览器。
1.在Ⅲ地址栏中,输入请求“/index.html”页面的命令:{{U}} (9) {{/U}}。
2.在IE地址栏中,输入关闭Web服务器的命令:{{U}} (10) {{/U}}。
【正确答案】
【答案解析】(1)ServerSocket(PORT)或ServerSocket(PORT,1,InetAddress.getByName (“127.0.0.1”)) (2)serverSocket. accept( ) (3)8etlnputStream( ) (4)getOutputStream( ) (5)Response(output( ) (6)sendStaticResource( ) (7)socket. close( ) (8)printStackTrace( ) (9)hnp://localhost:8080/index.html或http://127.0.0.1:8080/ index.html. (10)http://localhost:8080/shutdown或http://127.0.0.1: 8080/Shutdo