仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 463|回复: 11
打印 上一主题 下一主题

[学习教程] JAVA网页设计JSP - FAQ (4)

[复制链接]
只想知道 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:27:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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推出以后,性能上又有了很大提高。
莫相离 该用户已被删除
沙发
发表于 2015-1-18 22:25:35 | 只看该作者
Jive的资料在很多网站上都有,大家可以找来研究一下。相信你读完代码后,会有脱胎换骨的感觉。遗憾的是Jive从2.5以后就不再无条件的开放源代码,同时有licence限制。不过幸好还有中国一流的Java程序员关注它,外国人不开源了,中国人就不能开源吗?这里向大家推荐一个汉化的Jive版本—J道。Jive(J道版)是由中国Java界大名 鼎鼎的banq在Jive 2.1版本基础上改编而成, 全中文,增加了一些实用功能,如贴图,用户头像和用户资料查询等,而且有一个开发团队在不断升级。你可以访问banq的网站
活着的死人 该用户已被删除
板凳
发表于 2015-1-23 20:53:57 | 只看该作者
如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。
乐观 该用户已被删除
地板
发表于 2015-1-30 22:06:59 | 只看该作者
是一种为 Internet发展的计算机语言
山那边是海 该用户已被删除
5#
发表于 2015-1-31 07:55:10 | 只看该作者
设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧
飘飘悠悠 该用户已被删除
6#
发表于 2015-2-6 18:33:23 | 只看该作者
Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。
灵魂腐蚀 该用户已被删除
7#
发表于 2015-2-18 02:52:13 | 只看该作者
是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
蒙在股里 该用户已被删除
8#
发表于 2015-3-2 11:14:24 | 只看该作者
是一种将安全性(Security)列为第一优先考虑的语言
小魔女 该用户已被删除
9#
发表于 2015-3-6 20:03:41 | 只看该作者
是一种为 Internet发展的计算机语言
小女巫 该用户已被删除
10#
发表于 2015-3-10 12:04:35 | 只看该作者
是一种将安全性(Security)列为第一优先考虑的语言
飘灵儿 该用户已被删除
11#
发表于 2015-3-17 06:13:31 | 只看该作者
你快去找一份Java的编程工作来做吧(如果是在校学生可以去做兼职啊),在实践中提高自己,那才是最快的。不过你得祈祷在公司里碰到一个高手,而且他 还愿意不厌其烦地教你,这样好象有点难哦!还有一个办法就是读开放源码的程序了。我们知道开放源码大都出自高手,他们设计合理,考虑周到,再加上有广大的程序员参与,代码的价值自然是字字珠叽,铿锵有力(对不起,偶最近《金装四大才子》看多了)。
不帅 该用户已被删除
12#
发表于 2015-3-23 23:16:19 | 只看该作者
《JAVA语言程序设计》或《JAVA从入门到精通》这两本书开始学,等你编程有感觉的时候也可以回看一下。《JAVA读书笔记》这本书,因为讲的代码很多,也很容易看懂,涉及到面也到位。是你学习技术巩固的好书,学完后就看看《JAVA编程思想》这本书,找找一个自己写的代码跟书上的代码有什么不一样。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-28 19:24

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表