JSP技术方法及语法_第1页
JSP技术方法及语法_第2页
JSP技术方法及语法_第3页
JSP技术方法及语法_第4页
JSP技术方法及语法_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、出处:thinking in javawilliam gibsonjsp的技术方法为了快速方便地进行动态网站的开发,jsp在以下几个方面做了改进,使其成为快速建 立跨平台的动态网站的首选方案。1 .将内容的生成和显示进行分离用jsp技术,web页面开发人员可以使用html或者xml标识来设计和格式化最终 页面,并使用jsp标识或者小脚本来生成页面上的动态内容(内容是根据请求变化的,例如 请求账户信息或者特定的一瓶酒的价格等)。生成内容的逻辑被封装在标识和javabeans组 件中,并且捆绑在脚本中,所有的脚本在服务器端运行。由于核心逻辑被封装在标识和 javabeans屮,所以web管理人员和

2、页面设计者,能够编辑和使用jsp页而,而不影响内容 的生成。在服务器端,jsp引擎解释jsp标识和脚本,生成所请求的内容(例如,通过访问 javabeans组件,使用jdbc技术访问数据库或者包含文件),并且将结果以html (或者 xml)页面的形式发送冋浏览器。这既有助于作者保护自己的代码,又能保证任何基于 html的web浏览器的完全可用性。2. 可重用组件绝大多数jsp页血依赖于可重用的、跨平台的组件(javabeans或者enterprise javabeans 组件)來执行应用程序所要求的复杂的处理。开发人员能够共享和交换执行普通操作的组件, 或者使得这些组件为更多的使用者和客户团

3、体所使用。基于组件的方法加速了总体开发过 程,并且使得各种组织在他们现有的技能和优化结果的开发努力中得到平衡。3. 采用标识web页面开发人员不会都是熟悉脚本语言的编程人员。jsp技术封装了许多功能,这些 功能是在易用的、与jsp相关的xml标识中进行动态内容牛成所需要的。标准的jsp标识 能够访问和实例化javabeans组件,设置或者检索组件属性,下载applet,以及执行用其他 方法更难于编码和耗时的功能。4. 适应平台几乎所有平台都支持java, jsp+javabeans几乎可以在所有平台下通行无阻。从一个平 台移植到另外一个平台,jsp和javabeans甚至不用重新编译,因为ja

4、va字节码都是标准的 与平台无关的。5. 数据库连接java >i«连接数据库的技术是jdbc, java程序通过jdbc驱动程序与数据库相连,执行 查询、提取数据等操作。sim公司还开发了 jdbcodbc bridge,利用此技术jam程序可 以访问带有odbc驱动程序的数据库,目前大多数数据库系统都带有odbc驱动程序,所 以java程序能访问诸如oracle、sybase、ms sql server和ms access等数据库。此外,通过开发标识库,jsp技术可以进一步扩展。第三方开发人员和其他人员可以为 常用功能创建自己的标识库。这使得web页面开发人员能够使用熟悉的

5、工具和如同标识一 样的执行特定功能的构件来进行工作。jsp技术很容易整合到多种应用体系结构中,以利用现存的工具和技巧,并且能扩展到 支持企业级的分布式应用中。作为采用java技术家族的一部分,以及java 2 (企业版体系 结构)的一个组成部分,jsp技术能够支持高度复杂的基于web的应用。由于jsp页面的 内置脚本语言是基于java的,而ii所有的jsp页面都被编译成为java servlets,所以jsp页 血具有java技术的所有好处,包括健壮的存储管理和安全性。作为java平台的一部分,jsp 拥有java编程语言“一次编写,各处运行”的特点。jsp的应用模型利用jsp技术,动态信息由

6、jsp页面来表现,jsp页面由安装在web服务器或者使用 jsp的应用服务器上的jsp引擎执行。jsp引擎接受客户端对jsp页血的请求,并且生成jsp 页面作为对客户端的响应。jsp页面通常被编译成为java servlets,这是一个标准的java扩展。页面开发人员能够 访问全部的java应用环境,以利用java技术的扩展性和可移植性。当jsp页面第一次被调 用时,如果它还不存在,就会被编译成为一个java servlets类,并且存储在服务器的内存中。 这就使得在接下来的对该页面的调用中,服务器会有非常快的响应(这避免了 cgi-bin为 每个http请求生成一个新的进程的问题)。jsp页

7、面可以包含在多种不同的应用体系结构或者模型中,可以用于由不同协议、组件 和格式所组成的联合体屮。基于jsp的动态信息发布技术是一个开放的、可扩展的建立动态 web页面的标准。不论采用什么创建工具,开发人员都可以使用jsp页面来创建可移植的 web应用,在不同的web应用服务器上运行。jsp computing techniqueto carry on the dynamic website conveniently fast the development, jsp has made the improvement in the following several aspects, cause

8、s it to become builds the cross platform fast the dynamic website first choice plan.1. carries on the separation the con tent production and the demonstrationwith the jsp technology, the web page development personnel may use html or the xml marking design and the fonnatted final page, and uses the

9、jsp marking or the tootsy produces on page's dynamic content (content is originally acts according to request change, for example request account information or a specific bottle of liquor price and so on). production contents logic is sealed in marks and in the javabeans module, and ties up in

10、the script, all scripts in server end movement. because core logic is sealed in marks and in javabeans, therefore the web administrative personnels and the page designer, can edit and use the jsp page, but does not affect the content the production, in the server end, the jsp engine explained that t

11、he jsp marking and the script, produce the content which requested (e.g,through visits the javabeans module, uses jdbc technology visit database or included file), and (or xml) page's form transmits the result by html the browse匚 this both are helpful in the author protects own code, and can gua

12、rantee any based on the html web browser's complete usability2. may entrust with heavy responsibility the modulethe overwhelming majority jsp page relies on may ust with heavy responsibility, the cross platform module (javabeans or enteiprise the javabeans module) carries out complex processing

13、which the application procedure requests the development personnel can share and exchange the execution ordinary operation the module, or causes these modules uses for more users and the customer association has accelerated the overall development process based on modules method, and causes each kin

14、d of organization obtai ns balanced in their existing skill and in the optimized result development endeavor.3. uses markingthe web page development personnel will not be the familiar script language programmers. the jsp technology has sealed many functions, these functions are easy to use, marking

15、to carry on the dynamic content production with jsp in the related xml to need the standard jsp marking can visit and the instantiation javabeans module, the establishment or the retrieval module attribute, downloads applet, as well as the execution difficulty with codes and the time-consuming funct

16、ion with other methods.4. adapts the platformnearly all platforms support java, jsp+javabeans to be possible to pass unimpeded nearly under all platforms. transplants from a platform to other platform, jsp and javabeans does not even need to translate, because the java byte code is standard has noth

17、ing to do with the platform5. database connectionin java connects the database the technology is the jdbc, java procedure is connected through the jdbc driver and the database, operations and so on execution inquiry, extraction data. sun corporation has also developed jdbc-odbc bridge, uses this tec

18、hnical java procedure to be possible to visit has the odbc driver database, at present the majority database systems have the odbc driver, therefore the java procedure can visit such as oracle, sybase, ms sql server and databases and so on ms access, in addition, through the development marking stor

19、ehouse, the jsp technology may further expand the third party development personnel and other personnel may found their marking storehouse for the commonly used function. this enables the web page development personnel to be able to use the familiar tool and to be similar to marking same carries out

20、 the specific function component to carry on the work.the jsp technology very easy conformity to many kinds of application architecture, to use the extant tool and the skill, and can expand to the support enterprise distributional application. as uses the java technology family a part, as well as ja

21、va 2 (enteiprise version architecture) a constituent, the jsp technology can support highly complex based on the web application. because the jsp page's built-in script language is based on java, moreover all jsp page is translated into java servlets, therefore the jsp page has java technology a

22、ll advantage, including vigorous and healthy store management and security. as a java platform's part, jsp has the java programming language “a compilation, each place movement characteristicjsp application modelusing the jsp technology, the dynamic information displays by the jsp page, the jsp

23、page by installs, in the web server or uses jsp on the application servers jsp engine execution the jsp engine accepts the client side to the jsp page's request, and produces the jsp page to take to the client side response.the jsp page is usually translated into java servlets, this is a standar

24、d java expansion. the page development personnel can visit the complete java application environment, uses the java technology the extension and the probability when the jsp page first time is transferred, if it has not existed, will be translated into java the servlets kind, and will save in server

25、s memory. this causes in following to this pagefs transfer, the server could have the very quick response (this to avoid cgi-bin is each http request produces a new advancement question).the jsp page may contain in many kinds of different application architecture or the model, may use in the unified

26、 body which by the different agreement, the module and the form is composed based on the jsp dynamic information issue technology is one open, the expandable establishment dynamic web page's standard. no matter uses any foundation tool, the development personnel may use the web application which

27、 the jsp page founds may transplant, moves on the different web application server.jsp语法jsp文件(扩展名为.jsp)可以包含指令(或称为指示语句)、class-wide变量和方法、 直接插入的java代码(scriptlet)访问javabean变量数据的html模型、变量数据的 java表达式的组合。1jsp指令使用jsp指令(在v%和内的)来指定所使用的脚本语言、servlet实现的接口、 servlet扩展的类、servlet导入的软件包。jsp指令的一般语法形式为:%指令名二” 值” 。其中有效的

28、指令名为:(1) language:文件中所使用的脚本语言。此时对于java程序设计语言来说,仅java为 有效值和缺省值。该指令作用于整个文件。当多次使用该指令时,只有第一次使用是有效的。 示例如下:v% language 二"java" %。(2) method:由嵌入的java代码(scriptlet)生成的方法的名称。生成的代码会成为指 定方法名的主体。缺省的方法是serviceo当多次使用该指令时,只有第一次使用是有效的。 示例如下:v% method =hdopostu %。(3) import: servlet导入的java语言软件包名或类名列表,该列表是用逗

29、号分隔的。 在jsp文件中,可以多次指定该指令来导入不同的软件包。示例如下:v% import =,java.io.*java.util.hashtablen %°(4) content_type :生成的响应的mime类型。缺省值为text/html0当多次使用该指令 时,只有第一次使用是有效的.该指令可用以指定在其中对页而进行编码的字符集。示例 如下:v% content_type ="text/html; charset=gb2312" %。mplements:用于生成servlet实现的java语言接口列表,该列表是用逗号分隔的。 可以在一个jsp文件中多

30、次使用该命令,以实现不同的接口。示例如下:v% implements =mjavax.servlet.http.httpsessioncontexth %。(6) extends: servlet扩展的java语言类的名称。该类必须是有效的,且不能是一个 servlet类。该指令作用于整个jsp文件。当多次使用该指令时,只有第一次使用是有效的。 示例如下:v% extends =mjavax.servlet.http.httpservlet" %。2. class-wide变量和方法使用 vscript和/script标记來说明servlet类的class-wide变量和 class

31、-wide方法。一般语法如下:script runat=server o/ class-wide变量和方法的代码/script属性runat=server是必需的,它表明该标记是用于服务器端处理的。指定class-wide变 量和方法的示例如下:script runat=server/ class-wide 变量init i = 0;string foo = nhellou;/ class-wide 方v去private void foo() /该方法的代码</script>3. 访问 javabeanjsp对javabean的支持使您能通过web站点重复使用组件o javabea

32、n可以是类文件 或串行化bean,也可由servlet动态生成。请使用<bean>标记來创建一个bean实例, 以使从jsp文件的任何地方都可对该实例进行访问。标记<bean>的语法为:<bean name=hbean_nameu varname=nlocal_bean_namehtype =hclass_or_interface_namem introspect=myes|no"beanname=hser_filenamem create=hyes|nomscope=hrequest|session|userprofilen ><param

33、 property_name=,valueh></bean>其中的属性及其含义如下:(1) name:用于在相应的范围(由scope属性指定)内查找bean的名称。例如,这可 能是用于存储bean的会话(session)关键值。该值是分大小写的。(2) varname:用于在jsp文件中的任何地方引用bean的名称。该属性是可选的。缺 省值为name属性值。该值是分大小写的。(3) type: bean的类文件名。该名称用于以代码说明bean实例。缺省值为object类 型。该值是分大小写的。(4) introspect:当该值为真时,jsp处理器检查将所有的请求属性,并调用

34、与请求属性 匹配的set属性方法集(该属性是在beaninfo中传递的)。该屈性的缺省值为是。(5) beanname: bean的.class文件名、bean软件包名或包含bean的串行化文件(.scr 文件)的文件名。(该名称是给bean实例程序的)。仅当bean不在指定的范围中,且创 建属性被设置成是吋,才使用该属性。该值是分大小写的。文件路径必须在应用服务器的 java类路径中指定,除非文件在applicationserver_rootservlets目录下。(6) create:当该值为真时,如果处理器在指定的范圉内没有找到bean,则jsp将创建 一个bean实例。缺省值为真。(7

35、) scope: bean的生命期。该属性是可选的,其缺省值为request o有效值为:request -由servlet将bean设置成请求的上下文,该servlet使用jspapi中描述的 api调用jsp文件。如果bean不是请求上下文的一部分,则将创建该bean,并将其存 储在请求上下文中,除非创建属性己设置为否。session -如果当前对话屮存在bean,则bean已被重用。如果不存在bean,且创建 属性设置为是,则它已被创建并作为会话的一部分存储起来。userprofile -由servlet请求对象检索、造型成指定的类型,并进彳亍内省的用户简要表 文件。(在 ibm web

36、shere 应用服务器中,缺省类型为 com.ibm.servlet.personalization.userprofile.userprofile)o(8) param:属性和值配对的列表。属性是在bean屮用内省自动设置的。只在实例化 bean时,才对属性进行设置。除了使用vpaam>属性来设置bean属性外,还可以使用其它三种方法:第一,在请 求包含bean的web页面(jsp文件)的url时,指定查询参数。必须将内省属性设置 成"是其示例如卜: bean属性名将设置为joneso第二种方法,将属性指定成由html <from>标记提交的 参数。必须将molh

37、od属性设置成posto将action属性设置成调用bean的jsp文件的 urlo必须将introspect属性设置成“是”。其示例如下:<form action="/searchsite” method二"post"<input type=ntexth name=nsearch for: n>vinput type=hsubmith></form>第三中方法是使用jsp语法设置bean属性。在指定了 vbean>标记后,就可以从jsp文件中的任何地方访问beano这里有三种 访

38、问bean属性的方法:使用jspscriptlet使用jsp表达式、使用<insert>标记(如 同变量数据的html模板语法中所说明的)。请参阅jsp示例(displaydata.jsp文件)以 获取三个访问bean方法的每个方法示例。4. 直接插入的java代码(scriptlet)可以在jsp文件的<%和%>标记间直接嵌入任何有效的java语言代码。这样嵌入 的代码称为scriptleto如果没有指定method指令,则生成的代码将成为service方法的主 体。用于servlet的scriptlet可以使用一个预定义的变量集,该变量集符合基本的servlet&g

39、t; 输出和输入类:(1) request:由 javax.servlet.http.httpservletrequest 定义的 servlet 请求类(2) responses:由 javax.servlet.http.httpservletrequest 定义的 servlet 响应类(3) out: rfl java.io.printwriter定义的输岀转录程序类(4) in:由java.io.bufferedreader定义的输入阅读程序类其示例如下:<%foo = request.getparameterc'name");out.println(foo);

40、%>5. 变量数据的html模板语法当页面被返回给浏览器时,应用服务器html模板语法使您能将变量字段放在html 页面上,并使servlet和javabean可利用数据库的值动态地替换变量。该功能是jsp的 一个ibm扩展,它使引用变量数据变得十分容易。该语法只能用于jsp文件屮。html模 板语法包括:基本html模板语法;替代html模板语法;<repeat> 标记。这些标记通过html编写工具被设计成传递交互操作的标记。每一个标记有一个相应 的结束标记。每一个标记是分大小写的,它们的一些属性也是分大小写的。ibm websphere studio使开发jsp文件以包含

41、html模板语法变得更为容易。(1) 基本html模板语法<insert>标记是用于指定变量字段的基本标记。一般的语法为:<insert requestparm=pvalue requestattr=avalue bean=nameproperty=property_name(optional_index).subproperty _name(optio nal_index) default=value_when_null></insert>其中的屈性及其含义如下:requestparm:要在请求对象内进行访问的参数。该属性是分大小写的,并且不能与bean

42、和property属性一起使用。requestattr:要在请求对象内进行访问的属性。属性应使用setattribute方法设置。该 属性是分大小写的,并且不能与bean和property属性一起使用。bean:由<bean>标记在jsp文件屮说明的javabean名。请参阅访问javabean以 获得<bean>标记的解释。该属性的值是分大小写的。当指定了 bean属性,但未指定 property属性吋,在替换中将使用完整的bean。例如,如果bean是类型string且未指 定属性,则将替代string的值。property:访问替换的bean的属性。该属性的值是分

43、大小写的,并且是屈性的独立场 所名。该属性不能与requestparm属性一起使用。default:当bean属性值为空时,显示可选的字符串。如果字符串包含多个字,则该 字符串必须包含在一对双引号中(例如"helpdesk number")。该属性的值是分大小写的。 如果未指定值,则当属性值为空时,用空字符串替代。基本语法的示例如下:<insert bean=userprofile property=username></insert><insert requestparm=company default="ibm corporati

44、on"x/insert><insert requestattr=ceo default="company ceo"></insert>vinsert bean二userprofile property=lastconnectiondate.monthx/insert>在大多数情况下,property属性的值就是属性名。但是,可以通过指定property属性的 全格式来访问属性的某一属性(子属性)。这个全格式也提供选择项给您来指定索引属性的 一个索引。该可选的索引可以是一个常数(例如2)或如重复设置html标记屮描述的索 引。使用

45、属性标记的全格式示例如下:<insert bean=staffquery property=address(currentaddresslndex)x/insert><insert bean=shoppingcart property=items(4).price></insert><insert bean=foobean property=foo(2).bat(3).boo.far></insert>(2) 替代html模板语法html标准不允许在html标记中嵌入html标记。因而,就无法在另一个html 标记中嵌入vinsert

46、标记。作为代替,请使用html模板替代语法。要使用替代语法:请使用<insert>和v/insert>来包含html标记,在该标记中指出替代内容。指定bean和property属性:要指定bean和属性特性,请使用下列格式:$(bean=b property二p default二d),其中b、 p和d作为描述基本语法的值。要指定requestparm属性,请使用下列格式:$(requestparm=r default=d),其中r和d作 为描述基本语法的值。要指定requestattr属性,请使用下列格式:$(requestattr=r default=d),其屮i和d作 为

47、描述基本语法的值。替代html模板语法的示例如下:<insert><img src=$(bean=productads property=sale default=default.gif)></insert><insert><a href=" default=usa)&city$(requestparm=city default="research triangle park*1)&email=$(bean=userlnfo property=email)>show map of city</

48、a></insert>(3) <repeat>标记crepe at>标记的语法为:<repeat index=name start=starting_index end=ending_index></repeat>其中:index:是用于标识该重复数据块的一个可选的名称。该值是分大小写的。start:是用于该重复数据块的一个可选的开始索引值。缺省值为0。end:是用于该重复数据块的一个可选的结束索引值。最大值是2,147,483,647。如果结 束属性的值小于开始属性的值,则忽略结束属性。下面的示例1、2和3显示了如何使用<re

49、peat>标记。如果所有的索引属性拥有300 个或更少的元素,则这些示例将产生相同的输出。如果拥有的元素多于300个,示例1和 示例2将显示所有的元素,而示例3将只显示前300个元素。示例1用缺省开始和结束 索引显示了隐式索引:使用最小索引属性数的bean限制了循环重复的次数。<table><repeat><tr><td><insert bean=servicelocationsquery property=city></insert></tr></td><tr><td>

50、;<insert bean=servicelocationsquery property=address></insert></tr></td><tr><td><insert bean=servicelocationsquery property=telephone></insert></tr></td></repeat></table>示例2显示了索引、开始索引和结束索引:<table><repeat index=mylndex s

51、tart=0 end=2147483647><tr><tdxinsert bean=servicelocationsquery property=city(mylndex)></inserl></tr></td><tr><td><insert bean=servicelocationsquery property=address(mylndex)></insert></tr></td><tr><td><insert bean=s

52、ervicelocationsquery property=telephone(mylndex)x/insertx/tr></td></repeat></table>示例3用隐式开始索引显示了显式索引和结朿索引。虽然指定了索引属性,仍可对索 引过的屈性城市进行隐式索引,因为不需要(i)o<table><repeat index=mylndex end=299><tr><td><insert bean=servicelocationsquery property=city></insert

53、></tr></td><tr><td><insert bean=servicelocationsquery property=address(mylndex)>v/insert>v/tr></td><tr><td><insert bean=servicelocationsquery property=telephone(mylndex)x/insertx/tr></td></repeat></table>可以嵌套<repeat&

54、gt;数据块。独立索引每个数据块。该能力对交叉两个bean上的交错 属性或含有子属性的属性非常有用。在示例中,将两个<repeat>数据块嵌套,用以显示用 户购物手推车上每一张压缩光盘上的歌曲列表。<repeat index=cdindex><h 1>vinsert bean二shoppingcart property=cds.title></insert></h 1 ><table><repeat><tr><td><insert bean=shoppingcart prop

55、erty=cds(cdindex).playlist></insert></tdx/tr></table></repeat></repeat>6. 变量数据的java表达式当处理jsp文件时,要指定分解一个java语言表达式,请使用jsp表达式标记<%= 和>。该表达式被评估和转换成一个字符串,并加以显示。原语类型,如int和float都 自动转换成字符串表示法。在该示例屮,foo是在vscript>中加以说明的class-wide变 量,示例请见class-wide变量和方法:vp>翻译欢迎辞v%二 f

56、oo %>.</p>当获得jsp文件时,文本为:翻译欢迎辞hello。jsp grammarthe jsp document (expansion named .jsp) may contain the instruction (or is called instruction sentence), the class-wide variable and the method, the straight insertion java code (scriptlet), visits javabean, the variable data html model, the var

57、iable data java expression combinatio n.l. jsp instructionthe software package which uses the jsp instruction () to assign to use the script language which, servlet realize the connection which, servlet to expand the kind which, servlet induct. the jsp instruction general syntax form is: and effecti

58、ve instruction named:(1) language: in the document uses script language. this time regarding the java programming language, only java is the effective value and the default value. this instruction function in the entire document. when uses this instruction many times, only then the first use is effe

59、ctive the demonstration is as follows:.(2) method: method name which (scriptlet) produces by the inserting java code the production code will become assigns the side name in religion the main body. the default method is service. when uses this instruction many times, only then the first use is effec

60、tive. the dem on stration is as follows: (3) lmport:servlet inducts java the language software package name or the kind are tables, this tabulation is separates with the comma .in the jsp document, may assign this instruction to induct the different software package many times. the demonstration is as follows:.(4) content_type: production response mime type the default value is text/html. when uses this instruction many times, only then the first use is effective this instruction available assigns in carries on the code to the page the character

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论