灵魂腐蚀 发表于 2015-1-18 11:27:36

JAVA网站制作之JSP - FAQ (3)

其实产生见解的过程就是训练自己发现问题,分析问题的能力。根据以上的认识我想谈下传统的学习与通过视频独立学习的优缺点:js18)Whatdothedifferinglevelsofbeanstorage(page,session,app)mean?TOC



From:JoeShevland<J_Shevland@TurnAround.com.au>

ThespecisnotclearonwhattheApplicationlevelscopeactuallymeans,butthegeneraldiscussionhasitthatanApplicationisasingleJSPwhosebeanspersistfromcalltocall-unlikethosebeanswhichhavethe"page"scope.Thatsaid,the0.92specstillstores"application"beansattheservletlevelsotheycanactuallybeusedbymultipleservlets.

(FromGabrielWong<gabrielw@EZWEBTOOLS.COM>)
InpurelyServletterms,theymean:

page-NOstorage
session-servletrequest.getSession(true).putValue("myobjectname",myobject);
application-getServletConfig().getServletContext().setAttribute("myobjectname",myobject);
request-Thestorageexistsforthelifetimeoftherequest,whichmaybeforwardedbetweenjspsandservlets.
19)WherecanIfindthemailinglistarchives?TOC



ArchivesoftheJSPmailinglistareavailableathttp://archives.java.sun.com/archives/jsp-interest.html

Thesearchivesaresearchable.

20)WhataretheimportantstepsinusingJDBCinJSP?TOC



1)InstantiateaninstanceoftheJDBCdriveryouretryingtouse(jdbc-odbcbridgenamefrommemorysoplscheck):

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Thisdeterminesiftheclassisavailableandinstantiatesanewinstanceofit,makingitavailableforthenextstep.

2)AsktheDriverManagerforaConnectionobjectbasedontheJDBCURLyouareusing:

ConnectionconnDB=DriverManager.getConnection("jdbc:odbc:MyDSN",
"username","password");
DriverManagersearchesthroughanyregistereddrivers(instantiatinganewinstanceaboveisenoughtoregisteradriverwiththeDriverManager,aseachimplementationisrequiredto)and,basedontheJDBCURLyouareusing,returnstheappropriateimplementationofConnection.

3)CreateaStatementobjecttoretrieveaResultSet

StatementsmentDB=connDB.createStatement();
ResultSetrsDB=connDB.executeQuery("SELECT*FROMFoo");
or
rsDB=connDB.executeUpdate("UPDATEFooSETBar=NULL");
4)Closedownconnectionstofreeresources:

rsDB.close();
smentDB.close();
connDB.close();
NotesmentDB.close()closesthersDBobject,andconnDBwillclosesmentDB,cascadingdown,soyoucanreallyjust:connDB.Close().Alsonotetheresnoexceptionhandlinggivenhere.

WiththereleaseoftheJDBC2.0API,thereisaCachedResultSetcapabilitywhichwouldprovidesomeassistanceinmakingyourpagesperformbetter:

From:DIGNEMarc<jmdigne@MEUDON.NETCABLE.TM.FR>

ItestedtheJDBCCachedRowSethttp://developer.java.sun.com/developer/earlyAccess/crs/index.html

"JDBCTMCachedRowSetisanimplementationoftheRowsetinterface.TheRowsetinterfaceispartoftheJDBC2.0StandardExtensionAPI.

CachedRowSetprovidesadisconnected,serializable,scrollablecontainerfortabulardata.ACachedRowSetobjectcanbethoughtofasadisconnectedsetofrowsthatarebeingcachedoutsideofadatasource.

DatacontainedinaCachedRowSetmaybeupdatedandthenresynchronizedwiththeunderlyingtabulardatasource."

21)HowdoesvariablescopeworkinJSP?TOC



From:AlexanderYavorskiy<Alexander_Yavorskiy@VANTIVE.COM>

Hi,
AninterestingobservationaboutthevariablesdeclaredinJSPpages.

Anyvariabledeclaredinside<%....%>islocaltothepageandisnotvisibletooutsidefunctions,eventhosedeclareonthesameJSP.

Example:

<%
intevilVariable="666";
%>
...
functiontestFunction(){
//donotseeevilVariablefromhere
}
Why?evilVariableeventuallybecomesalocalvariableintheservice()methodoftheresultingservletandsoisnotaccessiblebyothermethodsofthatservlet.

Anyvariabledeclaredinside<%!%>becomeglobalforanyfunctiondeclaredintheservlet.

Example:
<%!
intevilVariable="666";
%>
...
functiontestFunction(){
intx=evilVariable;//cangettoit
}
Why?evilVariabledeclaredthiswaybecomesaprivatemembervariableoftheresultingservletandsoisaccessiblebyallothermethodsofthatservlet.

Conclusion

Itisimportanttounderstandthisdifferencebecauseinservletenvironmenttherewillonlybeasingle(!!!)instanceoftheresultingservletrunningandservingallrequestsforaparticularpage.Thus,potentiallyallofthemembervariablesofthatservletwillbeshareacrosstherequestsasopposedtovariableslocaltotheservice()methodthatwillberecreatedforeachrequest.So,weshouldbecarefulaboutputtingnoneconstantvariablesin<SERVER></SERVER>.Atthesametime,itmightbeusefultodosoinsomesituations.

22)HowdoIforwardtoanHTMLpage?TOC



Themethodforward()intheServletAPIworksforJSPpages,butthisisonlytrueforresourceswith_active_content,likeJSPpages.

IfyouwishtoforwardtoanHTMLpage,youhavetouseadifferentmethod:

From:VolkerStiehl<stiehl@ZNNBG.SIEMENS.DE>

InordertoaccessHTMLfilesyouhavetousethenew"resourceabstraction"featureof2.1.
Trythefollowing:

URLurl=getServletContext().getResource("/abc/xyz.html");
out.println(url.getContent());
23)ArethereanywhitepapersordocumentsexplaininghowJSPfits?TOC



From:"CraigR.McClanahan"<cmcclanahan@mytownnet.com>

http://www.software.ibm.com/ebusiness/pm.html

Itistitled"TheWebApplicationProgrammingModel",andprovidesaniceoverviewofthebasicarchitectureIBMproposesforwebapplications(essentiallythe"Model2"approachfromtheJSPspecification).TherearefewIBM-specificproductreferencesinthisdocument--simplytranslatetheirterm"dynamicserverpages"intoJSP,andgeneralize"WebSphere"toanyusefulcombinationofwebserver,servletengine,andappservercomponents.TherearemoreIBM-specificreferencesinseveraloftheotherwhitepapers,buttheystillprovideausefuloverviewofthetechnologybasisforlargescaleweb-basedapplicationdevelopmentanddeployment.Thewhitepaperindexisat:

http://www.software.ibm.com/ebusiness/library.html

24)HowtoIcreatedynamicGIFsformyJSP?TOC



From:MattiKotsalainen<matti@RAZORFISH.COM>

IfyouwanttocreateGIFs,useACMElabsexcellentfreegifencoder(http://www.acme.com/),andthendosomethinglikethis:

Frameframe=null;
Graphicsg=null;
FileOutputStreamfileOut=null;
try{
//createanunshownframe
frame=newFrame();
frame.addNotify();
//getagraphicsregion,usingtheframe
Imageimage=frame.createImage(WIDTH,HEIGHT);
g=image.getGraphics();
//manipulatetheimage
g.drawString("Helloworld",0,0);
//getanouputstreamtoafile
fileOut=newFileOutputStream("test.gif");
GifEncoderencoder=newGifEncoder(image,fileOut);
encoder.encode();
}catch(Exceptione){
;
}finally{
//cleanup
if(g!=null)g.dispose();
if(frame!=null)frame.removeNotify();
if(fileOut!=null){
try{fileOut.close();}
catch(IOExceptionioe){;}
}
}
25)DoyouknowwhereIcouldgetsomecodethatwouldencodesomethingtotheHTMLDTDstandard?TOC



Asamatteroffact...

(NB:ThisisanimplementationoftheHTMLEncodefunctionthatASPhas)

From:EricLunt<elunt@YAHOO.COM>

Somewhereinmynet-travelsIpickedupthisversionwhichperformsprettywell:

/**
*ReturnsanHTMLrenditionofthegiven<code>String</code>.Thiswas
*writtenby<ahref=mailto:kimbo@biddersedge.com>KimboMundy</a>.
*@paramtextA<code>String</code>tobeusedinanHTMLpage.
*@returnA<code>String</code>thatquotesanyHTMLmarkup
*characters.Ifnoquotingisneeded,thiswillbe
*thesameas<code>text</code>.
*/
publicstaticStringasHTML(Stringtext){
if(text==null)
return"";
StringBufferresults=null;
char[]orig=null;
intbeg=0,len=text.length();
for(inti=0;i<len;++i){
charc=text.charAt(i);
switch(c){
case0:
case&:
case<:
case>:
case":
if(results==null){
orig=text.toCharArray();
results=newStringBuffer(len+10);
}
if(i>beg)
results.append(orig,beg,i-beg);
beg=i+1;
switch(c){
default://case0:
continue;
case&:
results.append("&");
break;
case<:
results.append("<");
break;
case>:
results.append(">");
break;
case":
results.append(""");
break;
}
break;
}
}
if(results==null)
returntext;
results.append(orig,beg,len-beg);
returnresults.toString();
}
26)Whatispagecompilation?TOC



See(30)


你希望java的IDE整合。这个是没有必要的,重要的是你理解java有多深以及怎么组织你的代码,即使没有IDE,代码照样能够编译运行的。

金色的骷髅 发表于 2015-1-21 07:06:55

不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。

活着的死人 发表于 2015-1-30 10:35:28

接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。

飘灵儿 发表于 2015-2-6 09:59:00

那么我书也看了,程序也做了,别人问我的问题我都能解决了,是不是就成为高手了呢?当然没那么简单,这只是万里长征走完了第一步。不信?那你出去接一个项目,你知道怎么下手吗,你知道怎么设计吗,你知道怎么组织人员进行开发吗?你现在脑子里除了一些散乱的代码之外,可能再没有别的东西了吧!

再现理想 发表于 2015-2-7 18:44:41

http://www.jdon.com/去下载,或到同济技术论坛的服务器ftp://nro.shtdu.edu.cn去下,安装上有什么问题,可以到论坛上去提问。

仓酷云 发表于 2015-2-22 22:21:53

我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。

透明 发表于 2015-3-6 02:27:03

自从Sun推出Java以来,就力图使之无所不包,所以Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE,这也就是Sun ONE(Open Net Environment)体系。J2SE就是Java2的标准版,主要用于桌面应用软件的编程;J2ME主要应用于嵌入是系统开发,如手机和PDA的编程;J2EE是Java2的企业版,主要用于分布式的网络程序的开发,如电子商务网站和ERP系统。

莫相离 发表于 2015-3-12 18:49:39

一直感觉JAVA很大,很杂,找不到学习方向,前两天在网上找到了这篇文章,感觉不错,给没有方向的我指了一个方向,先不管对不对,做下来再说。

飘飘悠悠 发表于 2015-4-5 11:58:13

是一种语言,用以产生「小应用程序(Applet(s))
页: [1]
查看完整版本: JAVA网站制作之JSP - FAQ (3)