基于Jave的Web服務工作機制6_JSP教程
推薦:基于Jave的Web服務工作機制7sendStaticResource 方法是非常簡單的。它首先傳遞父路徑和子路徑給File類的構造器,從而對java.io.File類進行了實例化。 File file = new File(HttpServer.WEB_RO
parseUri 方法從請求行那里得到URI。Listing 1.3 展示了parseUri 方法的用途。 parseUri 減縮請求中的第一個和第二個空格來獲得URI。
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響應。它的構造函數(shù)接受一個OutputStream對象,比如下面的:
public Response(OutputStream output) {
this.output = output;
}
Response 對象被HttpServer類的await方法構造,該方法被傳遞的參數(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給出了它的實現(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"
"
File Not Found
";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();
}
}
分享:浮動菜單是如何作出來的mouse事件這個問題由我來做一個最終解答吧。我以前也同樣驚異于閃光地帶的這個特效,苦惱于不知如何實現(xiàn)。我在經(jīng)典提問,有一位網(wǎng)友熱心解答了我的問題,但只是局限于如何加入和“
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結示例
- JSP FusionCharts Free顯示圖表 具體實現(xiàn)
- 網(wǎng)頁模板:關于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關鏈接:
- 教程說明:
JSP教程-基于Jave的Web服務工作機制6
。