【正文】
goodies, such as images and 4 videos. The user clicks a link to make another request, and the process begins a new. Standard web servers handle static content and images quite well but need a helping hand to provide users with a customized, dynamic response. DEFINITION:Static content on the Web es directly from text or data files, like HTML or JPEG files. These files might be changed from time to time, but they are not altered automatically when requested by a web browser. Dynamic content, on the other hand, is generated on the fly, typically in response to an individualized request from a browser. Common Gateway Interface (CGI): The first widely used standard for producing dynamic content was the Common Gateway Interface (CGI). CGI uses standard operating system features, such as environment variables and standard input and output, to create a bridge, or gateway, between the web server and other applications on the host machine. The other applications can look at the request sent to them by the web server and create a customized response. When a web server receives a request that’s intended for a CGI program, it runs that program and provides the program with information from the ining request. The CGI program runs and sends its output back to the server. The web server then relays the response to the browser. CGI defines a set of conventions regarding what information it will pass as environment variables and how it expects standard input and output to be used. Like HTTP, CGI is flexible and easy to implement, and a great number of CGIaware programs have been written. The main drawback to CGI is that it must run a new copy of the CGIaware program for each request. This is a relatively expensive process that can bog down highvolume sites where thousands of requests are serviced per minute. Another drawback is that CGI programs tend to be platform dependent. A CGI program written for one operating system may not run on another. Java servlets: Sun’s Java Servlet platform directly addresses the two main drawbacks of CGI , servlets offer better performance and utilization of resources than 5 conventional CGI programs. Second, the writeonce, runanywhere nature of Java means that servlets are portable between operating systems that have a Java Virtual Machine (JVM). A servlet looks and feels like a miniature web server. It receives a request and renders a response. But, unlike conventional web servers, the servlet application programming interface (API) is specifically designed to help Java developers create dynamic applications. The servlet itself is simply a Java class that has been piled into byte code, like any other Java object. The servlet has access to a rich API of HTTPspecific services, but it is still just another Java object running in an application and can leverage all your other Java assets. To give conventional web servers access to servlets, the servlets are plugged into containers. The servlet container is attached to the web server. Each servlet can declare what URL patterns it would like to handle. When a request matching a registered pattern arrives, the web server passes the request to the container, and the container invokes the servlet. But unlike CGI programs, a new servlet is not created for each request. Once the container instantiates the servlet, it will just create a new thread for each request. Java threads are much less expensive than the server processes used by CGI programs. Once the servlet has been created, using it for additional requests incurs very little overhead. Servlet developers can use the init() method to hold references to expensive resources, such as database connections or EJB Home Interfaces, so that they can be shared between requests. Acquiring resources like these can take several seconds— which is longer than many surfers are willing to wait. The other edge of the sword is that, since servlets are multithreaded, servlet developers must take special care to be sure their servlets are threadsafe. To learn more about servlet programming, we remend Java Servlets by Example, by Alan R. Williamson [Williamson]. The definitive source for Servlet information is the Java Servlet Specification [Sun, JST]. JavaServer Pages: 6 While Java servlets are a big step up from CGI programs, they are not a panacea. To generate the response, developers are still stuck with using println statements to render the HTML. Code that looks like: (POne line of HTML./P)。構(gòu)架在這些公共基礎(chǔ)上的應(yīng)用框架可以為開發(fā)人員提供可以為他們的產(chǎn)品提供可重用服務(wù)的基礎(chǔ)架構(gòu)。 Struts以及本文中涉及的組件卻不是這樣。外交協(xié)議主要設(shè)計(jì)來避免誤解,以及防止談判破裂。這稱之為狀態(tài)協(xié)議。你甚至可以通過標(biāo)準(zhǔn)的程序如 Tel來手動傳遞一個(gè) HTTP請求。響應(yīng)的第一行稱為狀態(tài)行,包含了 HTTP協(xié)議的版本,數(shù)字型狀態(tài),以及狀態(tài)的簡短描述。這兩種方法都不是無縫的,是用哪一個(gè)都意味著在開發(fā)時(shí)都要進(jìn)行額外的工作。這些文件可以隨時(shí)改變,但通過瀏覽器請求時(shí),卻不能自動改變。與 HTTP一樣, CGI是靈活 和易于實(shí)現(xiàn)的,并且已經(jīng)有大量現(xiàn)成的 CGI程序。但,和常規(guī)web server 不同, servlet API 是專門設(shè)計(jì)來幫助 Java 開發(fā)人員創(chuàng)建動態(tài)應(yīng)用的。一旦容器實(shí)例化了一個(gè) servlet,它就僅為每個(gè)新的請求創(chuàng)建一個(gè)新的線程。為了產(chǎn)生響應(yīng),開發(fā)人員不得不使用大量的 println語句來生成 HTML。單獨(dú)使用 servlet 的做