基于Jave的Web服務(wù)工作機(jī)制(6)_Windows教程
Listing 1.3. The Request class' parseUri method
private String parseUri(String requestString) {
int index1, index2;
index1 = requestString.indexOf(' ');
if (index1 != -1) {
index2 = requestString.indexOf(' ', index1 + 1);
if (index2 > index1)
return requestString.substring(index1 + 1, index2);
}
return null;
}
Response類
Response表示一個HTTP響應(yīng)。它的構(gòu)造函數(shù)接受一個OutputStream對象,比如下面的:
public Response(OutputStream output) {
this.output = output;
}
Response 對象被HttpServer類的await方法構(gòu)造,該方法被傳遞的參數(shù)是從socket那里得到的OutputStream對象。
Response類有兩個公共方法: setRequest和sendStaticResource. setRequest方法傳遞一個Request對象給Response對象。Listing 1.4中的代碼顯示了這個:
Listing 1.4. The Response class' setRequest method
public void setRequest(Request request) {
this.request = request;
}
sendStaticResource 方法用來發(fā)送一個靜態(tài)資源,比如HTML文件。Listing 1.5給出了它的實(shí)現(xiàn)過程:
Listing 1.5. The Response class' sendStaticResource method
public void sendStaticResource() throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try {
File file = new File(HttpServer.WEB_ROOT, request.getUri());
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch != -1) {
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
}
else {
// file not found
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 23\r\n" +
"\r\n" +
"<h1>File Not Found</h1>";
output.write(errorMessage.getBytes());
}
}
catch (Exception e) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
}
finally {
if (fis != null)
fis.close();
}
}
Windows教程Rss訂閱服務(wù)器教程搜索
Windows教程推薦
- Apache服務(wù)器的安全性及實(shí)現(xiàn)(1)
- 在Windows系統(tǒng)上安裝PHP運(yùn)行環(huán)境文字教程
- Apache+php+Mysql在Windows下配置環(huán)境步驟說明
- OPENSSL服務(wù)_安全信息傳輸
- Apache服務(wù)器的安全性及實(shí)現(xiàn)(3)
- 深入剖析IIS 6.0(15)
- 通過內(nèi)核httpd實(shí)現(xiàn)web服務(wù)加速(1)
- 安全配置和維護(hù)Apache WEB Server(2)
- Windows服務(wù)器安全設(shè)置總結(jié)
- WIN2003服務(wù)器安全配置終極技巧(5)
- 相關(guān)鏈接:
- 教程說明:
Windows教程-基于Jave的Web服務(wù)工作機(jī)制(6)
。