国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

初學(xué)者入門:通過問答方式來認識學(xué)習(xí)JSP

2024-09-05 00:19:44
字體:
供稿:網(wǎng)友
1、如何混合使用jsp和ssi #include? 
在jsp中可以使用如下方式包含純html: 
<!--#include file="data.inc"--> 
但是如果data.inc中包含jsp code ,我們可以使用: 
<%@include file="data.inc"%> 


2、如何執(zhí)行一個線程安全的jsp? 
只需增加如下指令 
<%@ page isthreadsafe="false" %> 


3、jsp如何處理html form中的數(shù)據(jù)? 
通過內(nèi)置的request對象即可,如下: 
<% 
string item = request.getparameter("item"); 
int howmany = new integer(request.getparameter("units")).intvalue(); 
%> 


4、在jsp如何包含一個靜態(tài)文件? 
靜態(tài)包含如下:<%@ include file="copyright.html" %> 
動態(tài)包含如下:<jsp:include page="copyright.html" flush="true"/> 


5、在jsp中如何使用注釋? 
主要有四中方法: 
1。<%-- 與 --%> 
2。// 
3。/**與**/ 
4。<!--與--> 


6、在jsp中如何執(zhí)行瀏覽重定向? 
使用如下方式即可:response.sendredirect("http://ybwen.home.chinaren.com/index.html"); 
也能物理地改變http header屬性,如下: 
<% 
response.setstatus(httpservletresponse.sc_moved_permanently); 
string newlocn="/newpath/index.html"; 
response.setheader("location",newlocn); 
%> 


7、如何防止在jsp或servlet中的輸出不被browser保存在cache中? 
把如下腳本加入到j(luò)sp文件的開始即可: 
<% 
response.setheader("cache-control","no-store"); //http 1.1 
response.setheader("pragma","no-cache"); //http 1.0 
response.setdateheader ("expires", 0); //prevents caching at the proxy server 
%> 


8、在jsp中如何設(shè)置cookie? 
cookie是作為http header的一部分被發(fā)送的,如下方法即可設(shè)置: 
<% 
cookie mycookie = new cookie("aname","avalue"); 
response.addcookie(mycookie); 
%> 


9、在jsp中如何刪除一個cookie? 
<% 
cookie killmycookie = new cookie("mycookie", null); 
killmycookie.setmaxage(0); 
killmycookie.setpath("/"); 
response.addcookie(killmycookie); 
%> 


10、在一個jsp的請求處理中如何停止jsp的執(zhí)行 
如下例: 
<% 
if (request.getparameter("wen") != null) { 
// do something 
} else { 
return; 

%> 


11、在jsp中如何定義方法 
你可以定義方法,但是你不能直接訪問jsp的內(nèi)置對象,而是通過參數(shù)的方法傳遞。如下: 
<%! 
public string howbadfrom(httpservletrequest req) { 
httpsession ses = req.getsession(); 
... 
return req.getremotehost(); 

%> 
<% 
out.print("in general,lao lee is not baddie "); 
%> 
<%= howbadfrom(request) %> 


12、如果browser已關(guān)閉了cookies,在jsp中我如何打開session來跟蹤 
使用url重寫即可,如下: 
hello1.jsp 
<%@ page session="true" %> 
<% 
integer num = new integer(100); 
session.putvalue("num",num); 
string url =response.encodeurl("hello2.jsp"); 
%> 
<a href=<%=url%>>hello2.jsp</a> 

hello2.jsp 
<%@ page session="true" %> 
<% 
integer i= (integer )session.getvalue("num"); 
out.println("num value in session is "+i.intvalue()); 
%> 


13、在jsp中能發(fā)送email嗎 
可以使用sun的專用包:sun.net.smtp包。如下腳本使用smtpclient類發(fā)送email。 
<%@ page import="sun.net.smtp.smtpclient, java.io.*" %> 
<% 
string from="[email protected]"; 
string to="[email protected], [email protected]"; 
try{ 
smtpclient client = new smtpclient("mail.xxxxx.xxx"); 
client.from(from); 
client.to(to); 
printstream message = client.startmessage(); 
message.println("to: " + to); 
message.println("subject: sending email from jsp!"); 
message.println("this was sent from a jsp page!"); 
message.println(); 
message.println("cool! :-)"); 
message.println(); 
message.println("good boy"); 
message.println("im in genius.com"); 
message.println(); 
client.closeserver(); 

catch (ioexception e){ 
system.out.println("error sending email:"+e); 

%> 


14、在servlet中我能調(diào)用一個jsp錯誤頁嗎 
當然沒問題,如下展示了如何在一個servlet控制邏輯單元內(nèi)調(diào)用一個jsp錯誤頁面。 
protected void senderrorredirect(httpservletrequest request, 
httpservletresponse response, string errorpageurl, 
throwable e) 
throws servletexception, ioexception { 
request.setattribute ("javax.servlet.jsp.jspexception", e); 
getservletconfig().getservletcontext(). 
getrequestdispatcher(errorpageurl).forward(request, 
response); 


public void dopost(httpservletrequest request,httpservletresponse response) { 
try { 
// do something 
} catch (exception ex) { 
try { 
senderrorredirect(request,response,"/jsp/myerrorpage.jsp",ex); 
} catch (exception e) { 
e.printstacktrace(); 





15、jsp和applet如何通訊 
jsp如何與ejb sessionbean通訊 
下面的代碼段作了很好的示范 
<%@ page import="javax.naming.*, javax.rmi.portableremoteobject, 
foo.accounthome, foo.account" %> 
<%! 
//定義一個對sessionbeanhome接口實例的全局引用 
accounthome acchome=null; 

public void jspinit() { 
//獲得home接口實例 
initialcontext cntxt = new initialcontext( ); 
object ref= cntxt.lookup("java:comp/env/ejb/accountejb"); 
acchome = (accounthome)portableremoteobject.narrow(ref,accounthome.class); 

%> 
<% 
//實例化sessionbean 
account acct = acchome.create(); 
//調(diào)用遠程方法 
acct.dowhatever(...); 
// 如此等等 
%> 


16、當我使用一個結(jié)果集時,如何防止字段為"null"的字域顯示在我的html輸入文本域中? 
可以定義一個簡單的函數(shù)來達到目的,如下: 
<%! 
string blanknull(string s) { 
return (s == null) ? "" : s; 

%> 

然后在jsp的form中,可以這樣使用 
<input type="text" name="shoesize" value="<%=blanknull(shoesize)%>"> 


17、如何中servlet或jsp下載一個文件(如:binary,text,executable)? 
現(xiàn)提供兩個解決方案: 
a:使用http, 
b:在servlet中,通過設(shè)置contenttype和使用java.io包的stream等類可作到.例如: 
response.setcontenttype("application/x-msword"); 
然后想輸出緩沖中寫一些東東即可。 

18、使用usebean標志初始化bean時如何接受初始化參數(shù) 
使用如下兩標簽即可: 
<jsp:getproperty name="wenbean" property="someproperty"/> 
<jsp:setproperty name="wenbean" property="someproperty" value="somevalue"/> 

19、使用jsp如何獲得客戶瀏覽器的信息? 
使用request.getheader(string)即可 


20、能象調(diào)用子程序一樣調(diào)用jsp嗎? 
當然可以,用<jsp:include page="relativeurl" flush="true"/> 


21、當我重編譯我的jsp使用的一個類后,為什么jvm繼續(xù)使用我的老class? 


<%@include file="abc.jsp"%>與<jsp:include page="abc.jsp"/>之間的差別? 
前一個為靜態(tài)包含,而后一個為動態(tài)包含 


22、jsp的缺點? 
1。對java程序進行調(diào)試沒有好東東 
2。因大多數(shù)的servlet引擎不支持connection pooling 
3。servlet引擎沒有標準 
4。jsp與其它腳本語言的交互 


23、jsp能進行遞歸調(diào)用嗎? 
當然可以,如對form的提交給本頁 


34、如何實現(xiàn)jsp的國際化? 
為各種版本提供resource bundles屬性文件即可 

25、在jsp中如何寫文本文件? 
使用printwriter對象,如: 
<%@ page import="java.io.*" %> 
<% 
string str = "print me"; 
string nameoftextfile = "/usr/anil/imp.txt"; 
try { 
printwriter pw = new printwriter(new fileoutputstream(nameoftextfile)); 
pw.println(str); 
pw.close(); 
} catch(ioexception e) { 
out.println(e.getmessage()); 

%> 


26、如何在jsp中包括絕對路徑文件? 
使用urlconnection即可。 


27、在servlets和jsp之間能共享session對象嗎? 
當然可以, 
httpsession session = request.getsession(true); 
session.putvalue("variable","value"); 


28、javascript的變量能復(fù)制到j(luò)sp的session中嗎? 


29、如何設(shè)置cookie在某一時間后過期? 
用cookie.setmaxage(int) 


30、如何獲得當前的sessions數(shù)? 
可以使用httpsessionbindinglisteners來跟蹤 


31、能設(shè)置一些代碼在我所有的jsp文件之上運行?如果可以,能共享嗎? 
當然可以,可以為你的jsp文件定義一個別名:/jsp/=ybwen.genius.mypreprocessingservlet,而以/jsp/為前綴的文件可以使用 


32、對一個jsp頁,如果多個客戶端同時請求它,同步可能嗎? 
在jsp:usebean語法中使用beanname有何好處? 
beanname使用beans.instantiate()初始化bean 


33、當我使用<jsp:forward>時,在瀏覽器的地址欄沒有改變? 
使用response.sendredirect("newurl") 


34、如何轉(zhuǎn)換jsp 0.9版本的文件到j(luò)sp1.1? 
可使用sed/awk即可 


35、使用jsp能設(shè)置html form中輸入域的焦點,不用javascript? 
沒辦法 


36、使用jsp連接到數(shù)據(jù)庫連接緩沖池的最好方法是什么? 
1。使用jdbc2。0中帶有此服務(wù)的driver 
2.使用提供有此服務(wù)的application server 
3.自己寫
  • 本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 贵阳市| 阿克| 台中市| 盐池县| 东平县| 舒兰市| 湖口县| 涿州市| 景泰县| 进贤县| 大方县| 邻水| 廉江市| 丹江口市| 鹿泉市| 方正县| 乐昌市| 宿松县| 嘉义县| 大渡口区| 莒南县| 房产| 三门县| 兴城市| 永丰县| 比如县| 通榆县| 阿图什市| 台中市| 紫阳县| 乌拉特后旗| 客服| 黎平县| 石门县| 南阳市| 新竹市| 金塔县| 广河县| 武宣县| 小金县| 昌宁县|