![jsp學(xué)習(xí)ppt.ppt_第1頁](http://file1.renrendoc.com/fileroot2/2020-1/10/de1c6b6c-551e-40c8-8be3-39a6d98c7aab/de1c6b6c-551e-40c8-8be3-39a6d98c7aab1.gif)
![jsp學(xué)習(xí)ppt.ppt_第2頁](http://file1.renrendoc.com/fileroot2/2020-1/10/de1c6b6c-551e-40c8-8be3-39a6d98c7aab/de1c6b6c-551e-40c8-8be3-39a6d98c7aab2.gif)
![jsp學(xué)習(xí)ppt.ppt_第3頁](http://file1.renrendoc.com/fileroot2/2020-1/10/de1c6b6c-551e-40c8-8be3-39a6d98c7aab/de1c6b6c-551e-40c8-8be3-39a6d98c7aab3.gif)
![jsp學(xué)習(xí)ppt.ppt_第4頁](http://file1.renrendoc.com/fileroot2/2020-1/10/de1c6b6c-551e-40c8-8be3-39a6d98c7aab/de1c6b6c-551e-40c8-8be3-39a6d98c7aab4.gif)
![jsp學(xué)習(xí)ppt.ppt_第5頁](http://file1.renrendoc.com/fileroot2/2020-1/10/de1c6b6c-551e-40c8-8be3-39a6d98c7aab/de1c6b6c-551e-40c8-8be3-39a6d98c7aab5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Java Serve Page (JSP),Goals,The Java Server page (JSP) technology course provides students with the sound acknowledge to create web applications using JSP,Contents,Introduction to JSP Creating, deploying and executing a JSP JSP scripting JavaBeans for JSP Handling exceptions Advanced topics Custom t
2、ags JSTL Expression language Tag files,Skills You Get,Developing and deploying basic JSPs Handling exceptions Creating simple tags and tag files Developing complicated JSPs using JavaBeans, expression language, and custom tags especially JSTL tags Building web applications through tracking session,C
3、hapter 01Introduction to JSP,Why JSPDynamic Content,Static content Typically static HTML page Same display for everyone Dynamic content Contents is dynamically generated based on conditions Conditions could be User identity User entered values through forms or selections Etc.,An Overview of Web Appl
4、ication Development,Common Gateway Interfaces(CGI) CGI scripts execute program on the server Written in any language: Perl, C, C+, VB, etc. PHP Java servets JSP ASP,The Benefit of Servlets,Component-based, platform and server-independent Abundant third-party tool and server support Access to entire
5、family of Java API Performance and scalability Reliability No CGI limitations New process for each request Difficult to deal with concurrency, session and transaction,The Shortcomings of Servlets and CGI,Solutions prevent software reuse by combining with HTML and code Solutions require web designer
6、to have expertise in both web content and code development CGI-based Web Applications are difficult to maintain, non-scalable, non-manageable, and platform and application specific,What JSP Is,A text-based document capable of returning both static and dynamic content to a client browser Static and d
7、ynamic content can be intermixed Static content HTML, XML, Text Dynamic content Java code Display properties of JavaBeans Invoking business logic defined in custom tags JSP can access components,A Simple JSP, A simple JSP Hello, Guys! Current time is Black: static, Red: dynamic content,How to Invoke
8、 a JSP,Request,1.Http Request,Is JSP compiled and loaded?,compile,load,invoke,Run servlet,JSP Engine,2. no,2.,2. yes,3,4,5,6.Http Response,The Benefits of JSP,Content and display logic are separated Simplify web page development with JSP, JavaBeans and custom tags Support software reuse through the
9、use of components (JavaBeans, custom tags) Recompile automatically when changes are made to JSP pages Easier to author web pages Platform-independent Performance, scalability and Reliability Integrate into enterprise as part of J2EE,Why JSP over Servlet,Servlet can do a lot of things, but it is pain
10、 to Use those println() statement to generate HTML page Maintain that HTML page No need for compiling, packaging, CLASSPATH setting,Use JSP over Servlet or Vice-Versa,No, you want to use both leveraging the strengths of each technology Servlets strength is “controlling and dispatching” JSPs strength
11、 is “displaying” In a typical production environment, both servlet and JSP are used in a so-called MVC pattern Servlet handles controller part JSP handles view part,JSP import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class helloWorld_jsp extends org.apac
12、he.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent public String getServletInfo() return Your First Java Server Page; String helloWorld=Hello, World!; private static java.util.Vector _jspx_dependants; static _jspx_dependants = new java.util.Vector(1); _jspx_dependa
13、nts.add(/banner.html); public java.util.List getDependants() return _jspx_dependants; ,helloWorld_jsp.java(2),public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException JspFactory _jspxFactory = null; PageContext pageContext = null;
14、 HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType(text/html); pageContext
15、 = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out;,helloWorld_jsp.j
16、ava(3),out.write(rnrn); out.write(rn); out.write( Your first JSPrn); out.write( rn); out.write( ); out.write( ,JSP Scripting Elements,Let you insert Java code into the servlet that will be generated from JSP page Minimize the usage of JSP scripting elements in your JSP pages if possible Three forms:
17、 Expressions: Scriplets: Declarations: ,Expressions,During execution phase Expression is evaluated and converted into a String The String then insert into the servlets output stream directly Results in something like out.print(expression) Can use predefined variables (implicit objects) within expres
18、sion Format or Expression Semicolons are not allowed in expressions,expSample.jsp,Expression Sample Current time: Random number: Simple string: Simple statement: 1 + 1 = Visit implicit object: remote host is ,Segment of expSample_jsp.java,public void _jspService(HttpServletRequest request, HttpServl
19、etResponse response) throws java.io.IOException, ServletException out.write( Current time: ); out.print(new java.util.Date(); out.write(rn); out.write( Random number: ); out.print(Math.random(); out.write(rn); out.write( Simple string: ); out.print(Hello, world); out.write(rn); out.write( Simple sta
20、tement: 1 + 1 = ); out.print(1 + 1); out.write( rn); out.write( Visit implicit object: remote host is ); out.print(request.getRemoteHost(); out.write(rn); ,Scriplets,Used to insert arbitrary java code into servlets _jspService() method Can do things expressions alone can not do Executing code that c
21、ontains loops, conditionals Performing business logic or data access logic: updating database, writing server log, etc. Setting response header and status codes Can use predefined variables including implicit objects Format or Java code Usually ends with semi-colon,scrSample.jsp, Scriplet Sample Dis
22、play weekdays in Chinese: ); % ,Segment of scrSample_jsp.java,public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException . response.setContentType(text/html;charset=gb2312) ; java.text.DateFormatSymbols zhDfs = new java.text.DateFor
23、matSymbols(java.util.Locale.CHINA); String weekDays = zhDfs.getWeekdays(); for(int i = 1; i ); . ,Declarations,Used to define variables or methods that get inserted into the main body of servlet class (define classes?) Outside of _jspService() method Implicit objects are not accessible to declaratio
24、ns Usually used with expressions or scriplets For initialization and cleanup in JSP pages, use declarations to override jspInit() and jspDestroy() methods Format or method or variable declaration code ,decSample.jsp, Declaration Sample ); % ,Segment of decSample_jsp.java,public final class decSample
25、_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent String prompt = Display weekdays in Chinese:; String getChineseWeekdays() java.text.DateFormatSymbols zhDfs = new java.text.DateFormatSymbols(java.util.Locale.CHINA); return zhDfs.getWeekdays()
26、; public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException . ,Directives,Directives are messages to the JSP container in order to affect overall structure of the servlet Do not produce output into the current output stream Syntax
27、or ,Three Types of Directives,page: communicate page dependent attributes and communicate these to the JSP container include: used to include text or/and code at JSP page translation-time taglib: indicates a tag library that the JSP container should interpret ,The Page Directive,Defines a number of
28、page dependent properties and communicates these to the JSP container More than one instances of the page directive are permitted in a translation unit The pageEncoding and contentType attributes should appear at the beginning of the page, other attributes are position independent Syntax or ,The Pag
29、e Directive,Control ( important) Which scripting language is used in JSP page Super class of the servlet which results from the JSP page Which classes are imported whether participating in an (HTTP) session The buffering model for the initial out JspWriter to handle content output from the page ,The
30、 Page Directive,Control Whether the buffered content should be flushed automatically How multithread is handled (deprecated) The returned content of Servlet.getServletInfo() method Being intended to be the URL target of another JSP pagess errorPage ,The Page Directive,Control The URL to a resource w
31、hich processes error Which MIME type is generated Which character encoding used for the JSP page Whether EL expressions are ignored ,pageDireSample.jsp,Segment of pageDireSample_jsp.java,import java.util.*; import .*; public final class pageDireExample_jsp extends org.apache.jasper.runtime.HttpJspBa
32、se implements org.apache.jasper.runtime.JspSourceDependent public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException . _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType(text/html;charset=gb2312); pageContext =
33、_jspxFactory.getPageContext(this, request, response, error.jsp, false, 4096, false); ,Compare the Two,import java.util.*; import .*; public final class pageDireExample_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent public void _jspService(Ht
34、tpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException . _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType(text/html;charset=gb2312); pageContext = _jspxFactory.getPageContext(this, request, response, error.jsp, false, 4096, false);
35、,The Include Directive,Is processed when JSP page is translated into servlet Effect of the directive is to insert text contained in other file either static content or JSP page in the including JSP page Used to include banner content, copyright information, or any chunk of content that you may want
36、to reuse in other page Syntax and example or ,The Include Action,Is processed when a JSP is executed Allows you to include either static or dynamic resource in a JSP file Static: its content is insert into the calling JSP file Dynamic: the request is sent to the included resource, the included page
37、is executed, and then the result is included in the response from the calling JSP page Syntax and * ,inclActSample.jsp, Include Action Sample ,date.jsp, Hello, , ,Segment of inclActSample_jsp.java,import org.apache.jasper.runtime.JspRuntimeLibrary; . public void _jspService(HttpServletRequest reques
38、t, HttpServletResponse response) throws java.io.IOException, ServletException . out.write(rn); out.write( Your first JSPrn); out.write( rn); out.write( ); JspRuntimeLibrary.include(request, response, banner.html, out, false); out.write(rn); out.write( ); JspRuntimeLibrary.include(request, response,
39、date.jsp + (date.jsp).indexOf(?)0? . ,The Forward Action,Transfers control to another Web component from a JSP page If the page output buffered The buffer is cleared prior to forwarding The buffer was flushed, an attempt to forward the request will result in an IllegalStateException Syntax and * ,fw
40、dSample.jsp, helloWorld.jsp ,Segment of fwdSample_jsp.java,public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException . out.write(r); out.write(n); if (true) _jspx_page_context.forward(helloWorld.jsp); return; . ,The Parameter Actio
41、n,Provides key/value information Used in jsp:include, jsp:forward, jsp:params elements Used in jsp:include or jsp:forward, the included page or forwarded page will see the original request object, with the original parameters augmented with the new parameters, with new values taking precedence over
42、existing values when applicable Syntax ,fwdWithParamSample.jsp, ,sayHello.jsp, Say Hello Hello, The values of parameter user are : ,The Output of fwdWithParamSample.jsp,The Plugin Action,Embeds an object in the output page Syntax + arbitrary_text ,The Plugin Action,Sample unable to start plugin ,The
43、 Parameters Action,Just be a child of jsp:plugin Provides parameters to the Applet or JavaBeans component Syntax + ,The Fallback Action,Just be a child of jsp:plugin Indicates the content to be used by the client browser if the plugin cannot be started Syntax arbitrary_text,Other Actions,jsp:useBean
44、 jsp:setProperty jsp:getProperty jsp:attribute jsp:body jsp:doBody jsp:invoke jsp:element jsp:text jsp:output,Some of Them Will Be Discussed in Later Chapters,Think Beyond,There is only one instance of resulting servlet of a JSP page which runs in a Web application Variables declared in JSP declarat
45、ion are sharable among visitors, The Counter Page Welcome to the counter page. This page was visited times. counter.jsp, The Counter Page Welcome to the counter page. This page was visited times. safeCounter.jsp,Comments,There are two types of comments in JSP: Comments that document what the JavaSer
46、ver page is doing. The following is the syntax for these comments: or Comments that are sent as a response to users. The following is the syntax for these comments: or more comments . -,A Complex JSP-numberGuess.jsp(1),Number Guess Welcome to the Number Guess game. Congratulations! You got it. And a
47、fter just tries. Care to try again? ,A Complex JSP- numberGuess.jsp(2),Good guess, but nope. Try higher. answer) % lower. Im thinking of a number between 1 and 100. Whats your guess? ,Think Beyond Again,What will happen when more than one players play the guess number game concurrently?,Disadvantage
48、 and Guideline for Using JSP Scripting,Disadvantages: Overuse of scripting code can make JavaServer Pages confusing and difficult to maintain. Scripting code defeats two main JSP advantages: software reuse and separation of programming from content. Guidelines: Use scripting code only when component
49、 functionality is unavailable or when a JavaServer page requires limited scripting.,Chapter 04Working with Reusable Components,What are Software Components,Collections of useful, low-level APIs grouped into reusable programs that perform high-level tasks Major kinds of component JavaBeans Enterprise
50、 JavaBeans (EJB) ,API,API,API,API Library X,API Library Y,API,Component A,window.jsp,login,window,window,success,fail,Web Container,What are JavaBeans,Java classes that can be easily reused and composed together into an application Any Java class that follows certain design conventions can be JavaBe
51、ans component serializable zero-argument constructor properties of the class public methods to get and set properties communicating with others through events Within a JSP page, you can create and initialize beans and get and set the values of their properties JavaBeans can contain business logic or
52、 data base access logic,JavaBeans Design Conventions,JavaBeans maintain internal properties A property can be Read/write, read-only, or write-only Simple or indexed Properties should be accessed and set via getXxx and setXxx methods PropertyClass getProperty() setProperty(PropertyClass p) JavaBeans
53、must have a zero-argument (empty) constructor,What are Enterprise JavaBeans,A server-side component architecture for rapid and simplified development of distributed, secure, and portable enterprise applications, such as: Transaction processing Object-to-relational mapping Business logic encapsulatio
54、n Types of enterprise Beans: Session Beans Entity Beans Message-Driven Beans,JavaBeans versus Enterprise JavaBeans,JSP and Components,JSP page can access JavaBeans and Enterprise JavaBeans as needed JSP pages access JavaBeans by using scripting or actions JSP pages access EJB through JNDI (after lea
55、rning EJB),JavaBean Sample: NumberGussBean(1),package numberguess; import java.util.*; public class NumberGuessBean implements java.io.Serializable int answer; boolean success; String hint; int numGuesses; public NumberGuessBean() reset(); /1 method to set property “guess” public void setGuess(Strin
56、g guess) numGuesses+; int g; g = Integer.parseInt(guess); if (g = answer) success = true; else if (g answer) hint = lower; ,JavaBean Sample: NumberGussBean(2),/3 method to access properties “success”, “hint” and “guess” public boolean getSuccess() return success; public String getHint() return hint;
57、 public int getNumGuesses() return numGuesses; public void reset() answer = Math.abs(new Random(). nextInt() % 100) + 1; success = false; numGuesses = 0; ,Why Use JavaBeans in JSP Page,A JSP page can create and use any type of Java programming language object within a declaration or scriplet like th
58、e following: ,Why Use JavaBeans in JSP Page,JSP pages can use JSP actions to create and access the object that conforms to JavaBeans conventions: Create an instance of “NumberGuessBean” if none exists, stores it as an attribute of the session scope object, and makes the bean available throughout the session by the identifier “numguess”,Compare the Two, versus ,Why Use JavaBeans in JSP Page,No need to learn Java programming language for page designers Stronger separation between content and presentation Higher reusability of code Simple
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025車輛抵債合同書
- 2025煉化工程建設(shè)總承包合同
- 2025油漆工程承包合同
- 2024-2025學(xué)年新教材高中語文 第七單元 16.2 登泰山記說課稿(1)部編版必修上冊
- 2024-2025學(xué)年高中地理 第1章 旅游和旅游資源 第2節(jié) 旅游資源的類型說課稿 中圖版選修3
- 二手房交易時(shí)合同范例
- 飲料公司組建方案
- 《 負(fù)數(shù)》(說課稿)-2023-2024學(xué)年六年級下冊數(shù)學(xué)人教版
- 石材礦山起料方案
- 鑄造企業(yè)整治方案制定
- 喬遷新居結(jié)婚典禮主持詞
- 小學(xué)四年級數(shù)學(xué)競賽試題(附答案)
- 魯科版高中化學(xué)必修2全冊教案
- 建筑工程施工質(zhì)量驗(yàn)收規(guī)范檢驗(yàn)批填寫全套表格(浙江省)
- 《病理學(xué)基礎(chǔ)》知識考核試題題庫與答案
- 人口分布 高一地理下學(xué)期人教版 必修第二冊
- 部編版六年級下冊語文第3單元習(xí)作例文+習(xí)作PPT
- 四年級上冊英語試題-Module 9 Unit 1 What happened to your head--外研社(一起)(含答案)
- 子宮內(nèi)膜異位癥診療指南
- 《高級計(jì)量經(jīng)濟(jì)學(xué)》-上課講義課件
- 護(hù)理診斷及護(hù)理措施128條護(hù)理診斷護(hù)理措施
評論
0/150
提交評論