<%@ page contentType="text/html; charset=gb2312" import="java.io.*"%>
<%
try
{
//浣跨敤ServletContext瑁呭叆鏂囦欢璧勬簮
InputStream in=getServletContext().getResourceAsStream("/file.txt");
String file="";
int temp=0;
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
%>
13 涓В鍐虫柟妗?/h2>
#1
InputStream in=getServletContext().getResourceAsStream("/file.txt");
in=null?
#2
in搴旇涓嶄細绛変簬null鍟?绋嬪簭鍒板簳閿欏湪鍝噷浜嗗晩?
#3
涓轰粈涔堣鍦↗SP閲岄潰鍘诲啓杩欑浠g爜锛?
#4
鎴戞槸鍦ㄥ涔犵殑杩囩▼涓?杩欐槸涔︿笂鐨勪緥瀛?鍙槸涓嶇煡閬撲负浠€涔堜細鍑虹幇杩欐牱鐨勯敊璇?甯垜璋冭瘯涓嬪惂~
#5
file.txt鏀惧湪鏍圭洰褰曚笅锛?
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂
#6
濡傛灉绋嬪簭鏄繖涓€娈电殑璇?!娌℃湁姣?
in == null鐨勫彲鑳芥€уぇ鐨勪簡
鍏堝垽鏂璱n鏄惁==null
if(in!=null)
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?nbsp;
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
#7
寰堟槑鏄炬槸娌℃壘鍒版枃浠?鐩綍涓嶅,瀹炲湪涓嶈鐢ㄧ粷瀵硅矾寰?
#8
濂借薄鏄痠n涓虹┖鐨勫晩,闅鹃亾鏄?"/file.txt")鐨勯棶棰樺悧?file.txt涓庢垜鐨勬枃浠跺湪鍚屼竴涓洰褰曚笅鐨?
#9
/file.txt琛ㄧず鍦ㄦ牴鐩綍涓嬶紝浣犳槸鏀惧湪鏍圭洰褰曚笅鍚楋紵
#10
鍦ㄥ悓涓€涓洰褰曚笅灏变笉鐢ㄥ姞 /
#11
鎴戠幇鍦ㄥ彂鐜伴棶棰樹簡,瑕佸皢file鏂囨。鏀惧埌C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\ 鐨勪笅闈㈡墠鑳借В鍐抽棶棰?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?
#12
tomcat浠栭粯璁ょ殑鐩綍灏辨槸myapp\
#13
浣犳槑鐧戒粈涔堟槸鏍圭洰褰曞悧锛?/"灏变唬琛ㄦ牴鐩綍锛屼篃灏辨槸浣犺鐨刴yapp杩欎釜鐩綍锛岄偅涔堜綘鍐欑殑"/file.txt"灏变唬琛ㄦ槸myapp/file.txt!
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡
#1
InputStream in=getServletContext().getResourceAsStream("/file.txt");
in=null?
in=null?
#2
in搴旇涓嶄細绛変簬null鍟?绋嬪簭鍒板簳閿欏湪鍝噷浜嗗晩?
#3
涓轰粈涔堣鍦↗SP閲岄潰鍘诲啓杩欑浠g爜锛?
#4
鎴戞槸鍦ㄥ涔犵殑杩囩▼涓?杩欐槸涔︿笂鐨勪緥瀛?鍙槸涓嶇煡閬撲负浠€涔堜細鍑虹幇杩欐牱鐨勯敊璇?甯垜璋冭瘯涓嬪惂~
#5
file.txt鏀惧湪鏍圭洰褰曚笅锛?
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂
#6
濡傛灉绋嬪簭鏄繖涓€娈电殑璇?!娌℃湁姣?
in == null鐨勫彲鑳芥€уぇ鐨勪簡
鍏堝垽鏂璱n鏄惁==null
if(in!=null)
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?nbsp;
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
in == null鐨勫彲鑳芥€уぇ鐨勪簡
鍏堝垽鏂璱n鏄惁==null
if(in!=null)
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?nbsp;
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
#7
寰堟槑鏄炬槸娌℃壘鍒版枃浠?鐩綍涓嶅,瀹炲湪涓嶈鐢ㄧ粷瀵硅矾寰?
#8
濂借薄鏄痠n涓虹┖鐨勫晩,闅鹃亾鏄?"/file.txt")鐨勯棶棰樺悧?file.txt涓庢垜鐨勬枃浠跺湪鍚屼竴涓洰褰曚笅鐨?
#9
/file.txt琛ㄧず鍦ㄦ牴鐩綍涓嬶紝浣犳槸鏀惧湪鏍圭洰褰曚笅鍚楋紵
#10
鍦ㄥ悓涓€涓洰褰曚笅灏变笉鐢ㄥ姞 /
#11
鎴戠幇鍦ㄥ彂鐜伴棶棰樹簡,瑕佸皢file鏂囨。鏀惧埌C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\ 鐨勪笅闈㈡墠鑳借В鍐抽棶棰?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?
#12
tomcat浠栭粯璁ょ殑鐩綍灏辨槸myapp\
#13
浣犳槑鐧戒粈涔堟槸鏍圭洰褰曞悧锛?/"灏变唬琛ㄦ牴鐩綍锛屼篃灏辨槸浣犺鐨刴yapp杩欎釜鐩綍锛岄偅涔堜綘鍐欑殑"/file.txt"灏变唬琛ㄦ槸myapp/file.txt!
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡
#1
InputStream in=getServletContext().getResourceAsStream("/file.txt");
in=null?
in=null?
#2
in搴旇涓嶄細绛変簬null鍟?绋嬪簭鍒板簳閿欏湪鍝噷浜嗗晩?
#3
涓轰粈涔堣鍦↗SP閲岄潰鍘诲啓杩欑浠g爜锛?
#4
鎴戞槸鍦ㄥ涔犵殑杩囩▼涓?杩欐槸涔︿笂鐨勪緥瀛?鍙槸涓嶇煡閬撲负浠€涔堜細鍑虹幇杩欐牱鐨勯敊璇?甯垜璋冭瘯涓嬪惂~
#5
file.txt鏀惧湪鏍圭洰褰曚笅锛?
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂
#6
濡傛灉绋嬪簭鏄繖涓€娈电殑璇?!娌℃湁姣?
in == null鐨勫彲鑳芥€уぇ鐨勪簡
鍏堝垽鏂璱n鏄惁==null
if(in!=null)
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?nbsp;
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
in == null鐨勫彲鑳芥€уぇ鐨勪簡
鍏堝垽鏂璱n鏄惁==null
if(in!=null)
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//鍏抽棴杈撳叆娴併€?nbsp;
in.close();
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
#7
寰堟槑鏄炬槸娌℃壘鍒版枃浠?鐩綍涓嶅,瀹炲湪涓嶈鐢ㄧ粷瀵硅矾寰?
#8
濂借薄鏄痠n涓虹┖鐨勫晩,闅鹃亾鏄?"/file.txt")鐨勯棶棰樺悧?file.txt涓庢垜鐨勬枃浠跺湪鍚屼竴涓洰褰曚笅鐨?
#9
/file.txt琛ㄧず鍦ㄦ牴鐩綍涓嬶紝浣犳槸鏀惧湪鏍圭洰褰曚笅鍚楋紵
#10
鍦ㄥ悓涓€涓洰褰曚笅灏变笉鐢ㄥ姞 /
#11
鎴戠幇鍦ㄥ彂鐜伴棶棰樹簡,瑕佸皢file鏂囨。鏀惧埌C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\ 鐨勪笅闈㈡墠鑳借В鍐抽棶棰?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?
#12
tomcat浠栭粯璁ょ殑鐩綍灏辨槸myapp\
#13
浣犳槑鐧戒粈涔堟槸鏍圭洰褰曞悧锛?/"灏变唬琛ㄦ牴鐩綍锛屼篃灏辨槸浣犺鐨刴yapp杩欎釜鐩綍锛岄偅涔堜綘鍐欑殑"/file.txt"灏变唬琛ㄦ槸myapp/file.txt!
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡