jsp技術網(wǎng)站設計外文翻譯_第1頁
jsp技術網(wǎng)站設計外文翻譯_第2頁
jsp技術網(wǎng)站設計外文翻譯_第3頁
jsp技術網(wǎng)站設計外文翻譯_第4頁
jsp技術網(wǎng)站設計外文翻譯_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Combining JSP and ServletsThe technology of JSP and Servlet is the most important technology which use Java technology to exploit request of server, and it is also the standard which exploit business application .Java developers prefer to use it for a variety of reasons, one of which is already fami

2、liar with the Java language for the development of this technology are easy to learn Java to the other is "a preparation, run everywhere" to bring the concept of Web applications, To achieve a "one-prepared everywhere realized." And more importantly, if followed some of the princ

3、iples of good design, it can be said of separating and content to create high-quality, reusable, easy to maintain and modify the application. For example, if the document in HTML embedded Java code too much (script), will lead the developed application is extremely complex, difficult to read, it is

4、not easy reuse, but also for future maintenance and modification will also cause difficulties. In fact, CSDN the JSP / Servlet forum, can often see some questions, the code is very long, can logic is not very clear, a large number of HTML and Java code mixed together. This is the random development

5、of the defects. Early dynamic pages mainly CGI (Common Gateway Interface, public Gateway Interface) technology, you can use different languages of the CGI programs, such as VB, C / C + + or Delphi, and so on. Though the technology of CGI is developed and powerful, because of difficulties in programm

6、ing, and low efficiency, modify complex shortcomings, it is gradually being replaced by the trend. Of all the new technology, JSP / Servlet with more efficient and easy to program, more powerful, more secure and has a good portability, they have been many people believe that the future is the most d

7、ynamic site of the future development of technology. Similar to CGI, Servlet support request / response model. When a customer submit a request to the server, the server presented the request Servlet, Servlet responsible for handling requests and generate a response, and then gave the server, and th

8、en from the server sent to the customer. And the CGI is different, Servlet not generate a new process, but with HTTP Server at the same process. It threads through the use of technology, reduce the server costs. Servlet handling of the request process is this: When received from the client's req

9、uest, calling service methods, the method of Servlet arrival of the first judgement is what type of request (GET / POST / HEAD), then calls the appropriate treatment (DoGet / doPost / doHead) and generate a response. Although such a complex, in fact, simply said to Servlet is a Java class. And the g

10、eneral category of the difference is that this type operating in a Servlet container, which can provide session management and targeted life-cycle management. So that when you use the Servlet, you can get all the benefits of the Java platform, including the safety of the management, use JDBC access

11、the database and cross-platform capability. Moreover, Servlet using thread, and can develop more efficient Web applications. JSP technology is a key J2EE technology, it at a higher level of abstraction of a Servlet. It allows conventional static and dynamic HTML content generated by combining an HTM

12、L page looks like, but as a Servlet to run. There are many commercial application server support JSP technology, such as BEA WebLogic, IBM WebSphere, JRun, and so on. JSP and Servlet use more than simple. If you have a JSP support for Web servers, and a JSP document, you can put it Fangdao any stati

13、c HTML files can be placed, do not have to compile, do not have to pack, do not have to ClassPath settings, you can visit as ordinary Web It did visit, the server will automatically help you to do other work. JSP document looks like an ordinary static HTML document, but inside contains a number of J

14、ava code. It uses. Jsp the suffix, used to tell the server this document in need of special treatment. When we visit a JSP page, the document will first be translated into a JSP engine Java source files, is actually a Servlet, and compiler, and then, like other Servlet, from Servlet engine to handle

15、. Servlet engine of this type loading, handling requests from customers, and the results returned to the customer, as shown below: Figure 1: Calling the process of JSP pagesAfter another visit this page to the customer, as long as the paper there have been no changes, JSP engine has been loaded dire

16、ctly call the Servlet. If you have already been modified, it will be once again the implementation of the above process, translate, compile and load. In fact, this is the so-called "first person to punishment." Because when the first visit to the implementation of a series of the above pro

17、cess, so will spend some time after such a visit would not. Java servlets offer a powerful API that provides access to all the information about the request, the session, and the application. combining JSP with servlets lets you clearly separate the application logic from the presentation of the app

18、lication; in other words, it lets you use the most appropriate component type for the roles of Model, View and Controller.Servlets, Filters, and Listeners A servlet is a Java class that extends a server with functionality for processing a request and producing a response. It's implemented using

19、the classes and interfaces defined by the Servlet API. The API consists of two packages: the javax.servlet package contains classes and interfaces that are protocol-independent, while the javax.servlet.http package provides HTTP-specific extensions and utility classes.What makes a servlet a servlet

20、is that the class implements an interface named javax.servlet.Servlet, either directly or by extending one of the support classes. This interface defines the methods used by the web container to manage and interact with the servlet. A servlet for processing HTTP requests typically extends the javax.

21、servlet.http.HttpServlet class. This class implements the Servlet interface and provides additional methods suitable for HTTP processing. Servlet LifecycleThe web container manages all aspects of the servlet's lifecycle. It creates an instance of the servlet class when needed, passes requests to

22、 the instance for processing, and eventually removes the instance. For an HttpServlet, the container calls the following methods at the appropriate times in the servlet lifecycle.Besides the doGet( ) and doPost( ) methods, there are methods corresponding to the other HTTP methods: doDelete( ), doHea

23、d( ), doOptions( ), doPut( ), and doTrace( ). Typically you don't implement these methods; the HttpServlet class already takes care of HEAD, OPTIONS, and TRACE requests in a way that's suitable for most servlets, and the DELETE and PUT HTTP methods are rarely used in a web application. It

24、9;s important to realize that the container creates only one instance of each servlet. This means that the servlet must be thread safe - able to handle multiple requests at the same time, each executing as a separate thread through the servlet code. Without getting lost in details, you satisfy this

25、requirement with regards to instance variables if you modify the referenced objects only in the init( ) and destroy( ) methods, and just read them in the request processing methods.Compiling and Installing a ServletTo compile a servlet, you must first ensure that you have the JAR file containing all

26、 Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat includes it in a file called servlet.jar, located in the common/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH. Reading a RequestOne of the argumen

27、ts passed to the doGet( ) and doPost( ) methods is an object that implements the HttpServletRequest interface. This interface defines methods that provide access to a wealth of information about the request.Generating a ResponseBesides the request object, the container passes an object that implemen

28、ts the HttpServletResponse interface as an argument to the doGet( ) and doPost( ) methods. This interface defines methods for getting a writer or stream for the response body. It also defines methods for setting the response status code and headers.Using Filters and ListenersThe servlet specificatio

29、n defines two component types beside servlets: filters and listeners. These two types were introduced in the Servlet 2.3 specification, so if you're using a container that doesn't yet support this version of the specification, I'm afraid you're out of luck.FiltersA filter is a compon

30、ent that can intercept a request targeted for a servlet, JSP page, or static page, as well as the response before it's sent to the client. This makes it easy to centralize tasks that apply to all requests, such as access control, logging, and charging for the content or the services offered by t

31、he application. A filter has full access to the body and headers of the request and response, so it can also perform various transformations. One example is compressing the response body if the Accept-Language request header indicates that the client can handle a compressed response. A filter can be

32、 applied to either a specific servlet or to all requests matching a URL pattern, such as URLs starting with the same path elements or having the same extension.Listeners Listeners allow your application to react to certain events. Prior to Servlet 2.3, you could handle only session attribute binding

33、 events (triggered when an object was added or removed from a session). You could do this by letting the object saved as a sessionattribute(using the HttpSession.setAttribute() method)implement the HttpSessionBindingListener interface. With the new interfaces introduced in the 2.3 version of the spe

34、cification, you can create listeners for servlet context and session lifecycle events as well as session activation and passivation events (used by a container that temporarily saves session state to disk or migrates a session to another server). A new session attribute event listener also makes it

35、possible to deal with attribute binding events for all sessions in one place, instead of placing individual listener objects in each session.The new types of listeners follow the standard Java event model. In other words, a listener is a class that implements one or more of the listener interfaces.

36、The interfaces define methods that correspond to events. The listener class is registered with the container when the application starts, and the container then calls the event methods at the appropriate times.Initializing Shared Resources Using a ListenerBeans like this typically need to be initial

37、ized before they can be used. For instance, they may need a reference to a database or some other external data source and may create an initial information cache in memory to provide fast access even to the first request for data. You can include code for initialization of the shared resources in t

38、he servlet and JSP pages that need them, but a more modular approach is to place all this code in one place and let the other parts of the application work on the assumption that the resources are already initialized and available. An application lifecycle listener is a perfect tool for this type of

39、 resource initialization. This type of listener implements the javax.servlet.ServletContextListener interface, with methods called by the container when the application starts and when it shuts down.Picking the Right Component Type for Each TaskThe Project Billboard application introduced is a fairl

40、y complex application. Half the pages are pure controller and business logic processing, it accesses a database to authenticate users, and most pages require access control. In real life, it would likely contain even more pages, for instance, pages for access to a shared document archive, time sched

41、ules, and a set of pages for administration. As the application evolves, it may become hard to maintain as a pure JSP application. It's easy to forget to include the access control code in new pages. This is clearly an application that can benefit from using a combination of JSP pages and the co

42、mponent types defined by the servlet specification for the MVC roles. Let's look at the main requirements and see how we can map them to appropriate component types:l Database access should be abstracted, to avoid knowledge of a specific data schema or database engine in more than one part of th

43、e application: beans in the role of Model can be used to accomplish this.l The database access beans must be made available to all other parts of the application when it starts: an application lifecycle event listener is the perfect component type for this task. l Only authenticated users must be al

44、lowed to use the application: a filter can perform access control to satisfy this requirement. l Request processing is best done with Java code: a servlet, acting as the Controller, fits the bill. l It must be easy to change the presentation: this is where JSP shines, acting as the View. Adding serv

45、lets, listeners, and filters to the mix minimizes the need for complex logic in the JSP pages. Placing all this code in Java classes instead makes it possible to use a regular Java compiler and debugger to fix potential problems.Centralized Request Processing Using a ServletWith a servlet as the com

46、mon entry point for all application requests, you gain control over the page flow of the application. The servlet can decide which type of response to generate depending on the outcome of the requested action, such as returning a common error page for all requests that fail, or different responses d

47、epending on the type of client making the request. With the help from some utility classes, it can also provide services such as input validation, I18N preparations, and in general, encourage a more streamlined approach to request handling. When you use a servlet as a Controller, you must deal with

48、the following basic requirements: l All requests for processing must be passed to the single Controller servlet.l The servlet must be able to distinguish requests for different types of processing. Here are other features you will want support for, even though they may not be requirements for all ap

49、plications: l A strategy for extending the application to support new types of processing requests in a flexible manner.l A mechanism for changing the page flow of the application without modifying code.Mapping Application Requests to the ServletThe first requirement for using a Controller servlet i

50、s that all requests must pass through it. This can be satisfied in many ways. If you have played around a bit with servlets previously, you're probably used to invoking a servlet with a URI that starts with /myApp/servlet. This is a convention introduced by Suns Java Web Server (JWS), the first

51、product to support servlets before the API was standardized. Most servlet containers support this convention today, even though it's not formally defined in the servlet specification.將Servlet和JSP組合使用Servlet和JSP技術是用Java開發(fā)服務器端應用的主要技術,是開發(fā)商務應用表示端的標準。Java開發(fā)者喜歡使用它有多種原因,其一是對于已經(jīng)熟悉Java語言的開發(fā)者來說這個技術容易學習;其二

52、是Java把“一次編寫,到處運行”的理念帶入到Web應用中,實現(xiàn)了“一次編寫,到處實現(xiàn)”。而且更為重要的是,如果遵循一些良好的設計原則的話,就可以把表示和內容相分離,創(chuàng)造出高質量的、可以復用的、易于維護和修改的應用程序。比方說,在HTML文檔中如果嵌入過多的Java代碼(scriptlet),就會導致開發(fā)出來的應用非常復雜、難以閱讀、不容易復用,而且對以后的維護和修改也會造成困難。事實上,在CSDN的JSP/Servlet論壇中,經(jīng)??梢钥吹揭恍┨釂?,代碼很長,可以邏輯卻不是很清晰,大量的HTML和Java代碼混雜在一起,讓人看得一頭霧水。這就是隨意開發(fā)的弊端。早期的動態(tài)網(wǎng)頁主要采用CGI(C

53、ommon Gateway Interface,公共網(wǎng)關接口)技術,你可以使用不同的語言編寫CGI程序,如VB、C/C+或Delphi等。雖然CGI技術發(fā)展成熟且功能強大,但由于編程困難、效率低下、修改復雜等缺點,所以有逐漸被取代的趨勢。在所有的新技術中,JSP/Servlet具備更高效、更容易編程、功能更強、更安全和具有良好的可移植性,因而被許多人認為是未來最有發(fā)展前途的動態(tài)網(wǎng)站技術。與CGI相似,Servlet支持請求/響應模型。當一個客戶向服務器遞交一個請求時,服務器把請求送給Servlet,Servlet負責處理請求并生成響應,然后送給服務器,再由服務器發(fā)送給客戶。與CGI不同的是,S

54、ervlet沒有生成新的進程,而是與HTTP Server處于同一進程中。它通過使用線程技術,減小了服務器的開銷。Servlet處理請求的過程是這樣的:當收到來自客戶端的請求后,調用service方法,該方法中Servlet先判斷到來的請求是什么類型的(GET/POST/HEAD),然后調用相應的處理方法(doGet/doPost/doHead)并生成響應。別看這么復雜,其實簡單說來Servlet就是一個Java類。與一般類的不同之處是,這個類運行在一個Servlet容器內,可以提供session管理和對象生命周期管理。因而當你使用Servlet的時候,你可以得到Java平臺的所有好處,包括安

55、全性管理、使用JDBC訪問數(shù)據(jù)庫以及跨平臺的能力。而且,Servlet使用線程,因而可以開發(fā)出效率更高的Web應用。JSP技術是J2EE的一個關鍵技術,它在更高一級的層次上抽象Servlet。它可以讓常規(guī)靜態(tài)HTML與動態(tài)產(chǎn)生的內容相結合,看起來像一個HTML網(wǎng)頁,卻作為Servlet來運行。現(xiàn)在有許多商業(yè)應用服務器支持JSP技術,比如BEA WebLogic、IBM WebSphere、 JRun等等。使用JSP比用Servlet更簡單。如果你有一個支持JSP的Web服務器,并且有一個JSP文件,你可以把它放倒任何靜態(tài)HTML文件可以放置的位置,不用編譯,不用打包,也不用進行ClassPat

56、h的設置,就可以像訪問普通網(wǎng)頁那樣訪問它,服務器會自動幫你做好其他的工作。JSP 文件看起來就像一個普通靜態(tài)HTML文件,只不過里面包含了一些Java代碼。它使用.jsp的后綴,用來告訴服務器這個文件需要特殊的處理。當我們訪問一個JSP頁面的時候,這個文件首先會被JSP引擎翻譯為一個Java源文件,其實就是一個Servlet,并進行編譯,然后像其他Servlet一樣,由Servlet引擎來處理。Servlet引擎裝載這個類,處理來自客戶的請求,并把結果返回給客戶,如下圖所示:圖1: 調用JSP頁面的流程以后再有客戶訪問這個頁面的時候,只要該文件沒有發(fā)生過更改,JSP引擎就直接調用已經(jīng)裝載的Se

57、rvlet。如果已經(jīng)做過修改的話,那就會再次執(zhí)行以上過程,翻譯、編譯并裝載。其實這就是所謂的“第一人懲罰”。因為首次訪問的時候要執(zhí)行一系列以上的過程,所以會耗費一些時間;以后的訪問就不會這樣了。Java servlet提供了一種強有力的API,用這個API可以訪問關于請求、會話和應用程序的所有信息。將servlet和JSP頁面組合起來使用,可以把應用程序的邏輯部分和外觀呈現(xiàn)部分清楚地分開;換句話,利用這個方式可以對模型、視圖和控制器這三種角色分別使用最合適的組件類型。Servlet、過濾器和監(jiān)聽器Servlet是一種Java類,它使得服務器的功能可擴展至處理請求和生成應答。它是用Servlet

58、 API定義的類和接口實現(xiàn)的。API由兩個程序包組成:jvavax.servlet程序包包含獨立于協(xié)議的類和接口,而javax.servlet.http程序包則提供HTTP特定的擴展的實用程序類。Servlet的實質是實現(xiàn)了接口javax.servlet.Servlet的類,實現(xiàn)是直接完成或通過擴展某個支持類來完成的。該接口定義了Web容器用來管理servlet和與之交互的方法。用于處理HTTP請求的servlet一般情況下都會擴展javax.servlet.http.HttpServlet類。該類實現(xiàn)了Servlet接口,并提供了使用HTTP處理的附加方法。Servlet的生命周期Web容器

59、管理servlet生命周期的所有方面。它根據(jù)需要創(chuàng)建servlet類的實例、將請求傳遞給實例進行處理,最終刪除實例。對于HttpServlet來說,容器會在servlet生命周期的適當時間調用方法。除了doGet()和doPost()方法之外,還有一些對應于其他HTTP方法的方法:doDelete()、doHead()、doOptiongs()、doPut()和doTrace()。一般情況下不用實現(xiàn)這些方法,因為HttpServlet類已經(jīng)用適用于大多數(shù)servlet的方法考慮到了HEAD、OPTIONS和TRACE請求,而且DELETE和PUT這兩種HTTP方法很少用在Web應用程序中。容器只為每個Servlet創(chuàng)建一個實例非常重要。這意味著servlet必須是線程安全的即,能夠同時處理多個請求,每個處理都通過servlet代碼作為單獨的線程來執(zhí)行。如果只在init()和destroy()方法中修改參考的對象,而且只在請求處理方法中讀取他們,那么不用喪失任何細節(jié)就可以滿足關于實例變量的這個要求。編譯和安裝servlet要編譯servlet,必須首先確保JAR文件

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論