|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java的B/s开发是通常是javaweb开发,又叫J2EE开发,J2SE是手机开发。C#的C/s和B/s开发是说.net和Asp开发。。u在这里说明一点;资深一点的Java和C#程序员都明白一点js27)HowareservletsandJSPpagesrelated?TOC
JSPpagesarefocusedaroundHTML(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推出以后,性能上又有了很大提高。 |
|