<!-- #包括 file="data.inc"--> 1、如何混合使用jsp和ssi #include? 在jsp中可以使用如下方式包含純html: 但是如果data.inc中包含jsp code ,我們可以使用: <%@include file="data.inc"%> 2、如何執行一個線程安全的jsp? 只需增加如下指令 <%@頁面 isthreadsafe=& quot ;假的& quot ;%> 3、jsp如何處理html form中的數據? 通過內置的request對象即可,如下: <% 繩條款= request.getparameter("item"); int howmany =新建 integer(request.getparameter("units")).intvalue(); %> 4、在jsp如何包含一個靜態文件? 靜態包含如下: <%@ include file="copyright.html"%> 動態包含如下: <jsp:include page="copyright.html" lush="true"> 5、在jsp中如何使用注釋? 主要有四中方法: 1。 <%-- 與 --%> <!--與--> 2。// 3。/**與**/ 4。 6、在jsp中如何執行瀏覽重定向? 使用如下方式即可:response.sendredirect("http://ybwen.home.chinaren.com/index.html"); 也能物理地改變http header屬性,如下: <% response.setstatus(httpservletresponse.sc_moved_permanently); 串 newlocn="/newpath/index.html"; response.setheader("location",newlocn); %> 7、如何防止在jsp或servlet中的輸出不被browser保存在cache中? 把如下腳本加入到jsp文件的開始即可: <% response.setheader("cache-control","no-store"); // http 1.1 response.setheader("pragma","no-cache"); // http 1.0 response.setdateheader (& quot ;到期& quot ;,0);//阻止在代理服務器服務者緩沖 %> 8、在jsp中如何設置cookie? cookie是作為http header的一部分被發送的,如下方法即可設置: <% 小甜餅 mycookie =新建小甜餅(& quot ; aname & quot ;,& quot ; avalue & quot ;); response.addcookie(mycookie); %> 9、在jsp中如何刪除一個cookie? <% 小甜餅 killmycookie =新建小甜餅(& quot ; mycookie & quot ;,空); killmycookie.setmaxage(0); killmycookie.setpath("/"); response.addcookie(killmycookie); %> 10、在一個jsp的請求處理中如何停止jsp的執行 如下例: <% 如果 (request.getparameter("wen") !空的=){ //做一些東西 }另外{ 歸還; } %> 11、在jsp中如何定義方法 你可以定義方法,但是你不能直接訪問jsp的內置對象,而是通過參數的方法傳遞。如下: <%! 公共的繩 howbadfrom ( httpservletrequest req ){ httpsession ses = req.getsession(); ...... 歸還 req.getremotehost(); } %> <% out.print("in一般 ,老撾人的 lee不是 baddie & quot ;); %> <%= howbadfrom (請求 )%> 12、如果browser已關閉了cookies,在jsp中我如何打開session來跟蹤 使用url重寫即可,如下: hello1.jsp <%@頁面 session=& quot ;真實的& quot ;%> <% 整數 num =新建整數(100); session.putvalue("num",num); 繩 url =response.encodeurl("hello2.jsp"); %> <一 href=''<%=url%>''> hello2.jsp </a> hello2.jsp <%@頁面 session=& quot ;真實的& quot ;%> <% 整數i=(整數 )session.getvalue("num"); 在會議的 out.println("num價值是 "+i.intvalue()); %> 13、在jsp中能發送email嗎 可以使用sun的專用包:sun.net.smtp包。如下腳本使用smtpclient類發送email。 <%@頁面 import="sun.net.smtp.smtpclient, java.io.*" %> <% 串 from="[email protected]"; 繩 to="[email protected], [email protected]"; 試一試{ smtpclient顧客=新建 smtpclient("mail.xxxxx.xxx"); client.from(from); client.to(to); printstream消息= client.startmessage(); message.println("to: & quot ;+); 送電子郵件來自 jsp的 message.println("subject: !& quot ;); message.println("this被送來自一 jsp頁面 !& quot ;); message.println(); message.println("cool! :-)& quot ;); message.println(); message.println("good男孩& quot ;); 在 genius.com");的 message.println("i''m message.println(); client.closeserver(); } 抓住( ioexception e ){ 送電子郵件的 system.out.println("error :& quot ; +e ); } %> throwable e )request.setattribute ("javax.servlet.jsp.jspexception", e );getservletconfig().getservletcontext().getrequestdispatcher(errorpageurl).forward(request,}senderrorredirect(request,response,"/jsp/myerrorpage.jsp",ex);e.printstacktrace();}}} <%@頁面 import="javax.naming.*, javax.rmi.portableremoteobject, foo.accounthome, foo.account" %> <%! //定義一個對sessionbeanhome接口實例的全局引用 accounthome acchome=null; 公共的空的 jspinit (){ //獲得home接口實例 initialcontext cntxt =新建 initialcontext ( ); 對象 ref= cntxt.lookup("java:comp/env/ejb/accountejb"); acchome = (accounthome)portableremoteobject.narrow(ref,accounthome.class); } %> <% //實例化sessionbean 帳目 acct = acchome.create(); //調用遠程方法 acct.dowhatever (...); // 如此等等 %> 16、當我使用一個結果集時,如何防止字段為"null"的字域顯示在我的html輸入文本域中? 可以定義一個簡單的函數來達到目的,如下: <%! 繩 blanknull (串 s ){ 回來(空的 s ==)?"": s; } %> 然后在jsp的form中,可以這樣使用 <輸入類型="文章"命名="shoesize"value=" <% =blanknull ( shoesize )%> "> response.setcontenttype("application/x-msword"); < jsp:getproperty命名=& quot ; wenbean & quot ;property=& quot ; someproperty & quot ;/> < jsp:setproperty命名=& quot ; wenbean & quot ;property=& quot ; someproperty & quot ;value=& quot ; somevalue & quot ;/> 19、使用jsp如何獲得客戶瀏覽器的信息? 使用request.getheader(string)即可 20、能象調用子程序一樣調用jsp嗎? 當然可以,用 <jsp:include page="relativeurl" lush="true"> 21、當我重編譯我的jsp使用的一個類后,為什么jvm繼續使用我的老class? <%@include file="abc.jsp"%> 與 <jsp:include page="abc.jsp"> 之間的差別? 前一個為靜態包含,而后一個為動態包含 22、jsp的缺點? 1。對java程序進行調試沒有好東東 2。因大多數的servlet引擎不支持connection pooling 3。servlet引擎沒有標準 4。jsp與其它腳本語言的交互 23、jsp能進行遞歸調用嗎? 當然可以,如對form的提交給本頁 34、如何實現jsp的國際化? 為各種版本提供resource bundles屬性文件即可 25、在jsp中如何寫文本文件? 使用printwriter對象,如: <%@頁面 import="java.io.*" %> <% 繩 str =& quot ;打印我& quot ;; 繩 nameoftextfile = "/usr/anil/imp.txt"; 試一試{ printwriter pw =新建 printwriter (新建 fileoutputstream ( nameoftextfile )); pw.println(str); pw.close(); }抓住( ioexception e ){ out.println(e.getmessage()); } %> session.putvalue("variable","value"); <jsp:forward> 時,在瀏覽器的地址欄沒有改變? 使用response.sendredirect("newurl") 34、如何轉換jsp 0.9版本的文件到jsp1.1? 可使用sed/awk即可 35、使用jsp能設置html form中輸入域的焦點,不用javascript? 沒辦法 36、使用jsp連接到數據庫連接緩沖池的最好方法是什么? 1。使用jdbc2。0中帶有此服務的driver 2.使用提供有此服務的application server 3.自己寫 |
新聞熱點
疑難解答