JAVA网页设计JSP - FAQ (4)
Java的B/s开发是通常是javaweb开发,又叫J2EE开发,J2SE是手机开发。C#的C/s和B/s开发是说.net和Asp开发。。u在这里说明一点;资深一点的Java和C#程序员都明白一点js27)HowareservletsandJSPpagesrelated?TOCJSPpagesarefocusedaroundHTML(orXML)withJavacodesandJSPtagsinsidethem.WhenawebserverthathasJSPsupportisaskedforaJSPpage,itcheckstoseeifithasalreadycompiledthepageintoaservlet.Thus,JSPpagesbecomeservletsandaretransformedintopureJavaandthencompiled,loadedintotheserverandexecuted.DifferentJSPimplementationsdothisinmoreorlessefficientways.
28)AnygoodwebsitesforuptodateactivitiesintheJava/JSP/Servletworld?TOC
ThefollowingwebsitescontaininformationaboutJSP:
AnIBMTutorialonJSP:http://www.software.ibm.com/developer/education/java/online-courses.html
AnIBMRedBook:http://www.redbooks.ibm.com/abstracts/sg245423.html
OtherIBMInformation:http://www.software.ibm.com/webservers/appserv/doc/v20dcadv/doc/index.html
JSP-ResourceInformation-http://www.jspin.com/isquitecomprehensiveonsitesandarticles.
JSPTagsisasiteforTaglibs-http://jsptags.com/
ThefollowingwebsitesfocusonJSPsolutions
ServletsTaverne-http://www.interpasnet.com/JSS/
OiServletWorld-http://i.am/servletforme
WebDevelopmentwithJSP-http://www.burridge.net/jsp/
29)HowdoIforceausertologin?TOC
From:AndreRichards<AndreRic@MWEB.CO.ZA>
Ididasfollows:
Oneverypagewhichmustbeauthenticated,IcheckforauserIDinthesessionobject-ifitdoesntexit,Idoaredirecttoaloginpage,passingtheurltheuserwastryingtoaccessasaparameter.
Ontheloginpage,iftheusersuccessfullylogsin,Icreateasessionforhim/her,andaddtheuserIDtothesession.Ithenredirectbacktotheoriginalpagetheusertriedtoaccess.Thisway,eveniftheuserbookmarksapage,he/shewillbeaskedtologinoncethesessionhasbecomeinvalid.
Somecode:
OneverypageIaddthefollowing:
HttpSessionsession=request.getSession(true);
if(session.getValue("CustomerID")==null){
response.sendRedirect(response.encodeRedirectUrl
("Login.jsp?Origin=SharePortfolio.jsp"));
}
else{
//therestofthepage...
InLogin.jsponcetheuserhasprovidedthecorrectlogoncredentials:
session.putValue("CustomerID",CustomerID);
response.sendRedirect(response.encodeRedirectUrl(request.getParameter("Origin")));
--------------------------------------------------------------------------------
Anotherdeveloperhasadifferentapproach:
From:ChristopherCobb<ccobb@usgs.gov>
Afterresearchingseveralapproaches,Ihavefinallysettledonthefollowingapproach.Iwouldliketohearhowothers
aresolvingthisproblem.(FAQmaintainersnote:ThissyntaxwontworkwithJSP1.0)
1.UseraccessesGuardedPage.jspvia
http://localhost/path/to/GuardedPage.jsp
2.GuardedPage.jspincludesalogincheckingpage:
<!--#includefile="/admin/"file="LoginChecker.jsp"-->
Everypagethatneedstobelogin-protectedshouldincludethisfile(which,dependingonhowyoursiteissetup,maybe
everypage.)
3.LoginChecker.jspaccessesabeanthatdoestheloginchecking:
<USEBEANlifespan="session"name="loginChecker"type="package.LoginChecker">
<setfromrequestbeanproperty="*">
</USEBEAN>
4.TheLoginCheckerbeanhasapropertyloggedIn.(ItalsohasproperiesforUsernameandPassword,anda
processRequest()method,whichareusedlater).
LoginChecker.jspchecksthevalueoftheloggedInproperty.Ifitisnottrue(i.e.,theuserisnotloggedin),alogin
pageisdisplayed:
<excludeifproperty="loginChecker:loggedIn"value="true">
<FORMaction="/servlet/DBAccess/path/to/GuardedPage.jsp"method="post">
Username:<inputname="userName"size="15"maxlength="15">
Password:<inputtype="password"name="password"size="15"maxlength="15">
<inputtype="submit"name="loginUser"value="Submit">
</FORM>
</excludeif>
Thefirsttimethrough,thisbeanwillbeemptyandtheloggedInpropertywillnotbeset.Theloginformwilltherefore
bedisplayed.
5.Thereisalittletrickintheactionclauseabove.Whentheusertypesinhislogininfoandpressessubmit,the
invokedURLis
/servlet/DBAccess/path/to/GuardedPage.jsp
TheactionpassesthroughtheservletDBAccess,thencontinuesontoouroriginalpage.Thisservletdoesnothingmore
thanattachanopendatabaseconnectiontothecurrentsession:
session.putValue("open.connection",connection);
TheservletthenpicksupthetrailingpartoftheURLwith:
StringtrailingURL=request.getPathInfo();
Itthencallsforward()topasscontrolbacktotherequestedpage.Inthisexample,thenewpagehappenstobethesame
asthepagewecamefrom.
getServletConfig(
).getServletContext(
).getRequestDispatcher(response.encodeURL(trailingURL)
).forward(request,response);
6.NowwearebacktoouroriginalpageandthelogginCheckerbeangetsinvokedagain.Becauseofthe:
<setfromrequestbeanproperty="*">
intheloginCheckerUSEBEANtag,andbecauseourusernameandpasswordfieldnamesintheLoginChecker.jsppagematchour
beanspropertynames,theusernameandpasswordthattheusertypedingetmagicallypopulatedinthecorresponding
propertiesofthebean.
7.TheLoginCheckerbeanhasaprocessRequest()methodwhichcheckstoseeifausernameandpasswordhasbeensupplied.
Ifso(andifwearenotloggedin),itperformsadatabaselookuptologtheuserin.Ifthelookupissuccessful,the
loggedInpropertyissettotrue.
8.WearefinallybacktoourGuardedPage.jsppage.Itwillprobablynotwanttodisplayitselfunlesstheuserislogged
in.ThepageshouldthereforeonlybeincludedifloggedInistrue:
<includeifproperty="loginChecker:loggedIn"value="true">
ThecontentsofGuardePage.jsparedisplayedonlyifloggedInistrue.
</includeif>
Weredone!GuardedPage.jspisonlydisplayediftheuserisloggedin.Iftheuserisnotloggedin,aloginpageis
displayed,whichifsuccessful,returnstheusertotheoriginalpage.
9.ThereisonesmallcleanupwhichisneededinStep4.Ascodedabove,apassthroughservletisusedtoattacha
databaseconnectiontothesession.Iftheuserrepeatedlyfailstologin,theservletprefixwillgetrepeatedly
pre-pendedtotheURL.Furthermore,thecurrentpageishardcodedintotheLoginChecker.jsppagewhichrestrictsits
reusability.AlittleJavaScriptfixesbothoftheseproblems.ThefollowingJavaScriptshouldbeusedinplaceofthe
<FORM>taginStep4.above.
<scriptlanguage="JavaScript">
<!--
if(document.location.pathname.indexOf("/servlet/package.DBAccess")==0)
document.write(
<FORMaction="+
document.location.pathname+
"method="post">);
else
document.write(
<FORMaction="/servlet/package.DBAccess+
document.location.pathname+
"method="post">);
//-->
</script>
30)SohowcananewbiegetstartedwithJSP?TOC
SeetheQuickStartsectionoftheJSPBookathttp://www.esperanto.org.nz/jspbook
31)HowcanIensurethatsessionobjectsstayinexistencewhenthewebserverrestarts?TOC
ThereisnorequirementthatasessionobjectwillstayaroundasfarasIcantell,butsomewebserverswillserializeobjectsiftheysupporttheserializationinterface.
32)HowcanIincludeoneJSPinsideanotherJSP?TOC
JRUN,ServletExecandGNUJSPallowyoutospecify(itwasinthe0.91spec):
<%@include="./header.jsp"%>-whereheader.jspisthefileyouwanttoinclude.
ThespecdoessaythatitsupportsNCSAstyleincludesasin
<!--#includevirtual="/pathfromdocdir/"file="copyright.html"-->
<!--#includefile="data/table.html"-->
ButthereisnorequirementthattheysupportJSP.
先说优点,首先和C,C++这些语言比起来,java很简单,去掉指针的java,非常好理解,自动垃圾回收机制也很好,自从JDK1.5推出以后,性能上又有了很大提高。 Jive的资料在很多网站上都有,大家可以找来研究一下。相信你读完代码后,会有脱胎换骨的感觉。遗憾的是Jive从2.5以后就不再无条件的开放源代码,同时有licence限制。不过幸好还有中国一流的Java程序员关注它,外国人不开源了,中国人就不能开源吗?这里向大家推荐一个汉化的Jive版本—J道。Jive(J道版)是由中国Java界大名 鼎鼎的banq在Jive 2.1版本基础上改编而成, 全中文,增加了一些实用功能,如贴图,用户头像和用户资料查询等,而且有一个开发团队在不断升级。你可以访问banq的网站 如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。 是一种为 Internet发展的计算机语言 设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧 Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。 是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言 是一种将安全性(Security)列为第一优先考虑的语言 是一种为 Internet发展的计算机语言 是一种将安全性(Security)列为第一优先考虑的语言 你快去找一份Java的编程工作来做吧(如果是在校学生可以去做兼职啊),在实践中提高自己,那才是最快的。不过你得祈祷在公司里碰到一个高手,而且他 还愿意不厌其烦地教你,这样好象有点难哦!还有一个办法就是读开放源码的程序了。我们知道开放源码大都出自高手,他们设计合理,考虑周到,再加上有广大的程序员参与,代码的价值自然是字字珠叽,铿锵有力(对不起,偶最近《金装四大才子》看多了)。 《JAVA语言程序设计》或《JAVA从入门到精通》这两本书开始学,等你编程有感觉的时候也可以回看一下。《JAVA读书笔记》这本书,因为讲的代码很多,也很容易看懂,涉及到面也到位。是你学习技术巩固的好书,学完后就看看《JAVA编程思想》这本书,找找一个自己写的代码跟书上的代码有什么不一样。
页:
[1]