280 likes | 507 Views
Understanding Java servlets. 鄧姚文 joseph.deng@gmail.com. Outline. What is a servlet? What is a servlet container? Hello World servlet The relationship between a servlet container and the servlet API. 什麼是 Servlet?. A server-side entity A new design pattern for writing servers
E N D
Understanding Java servlets 鄧姚文 joseph.deng@gmail.com
Outline What is a servlet? What is a servlet container? Hello World servlet The relationship between a servlet container and the servlet API
什麼是 Servlet? • A server-side entity • A new design pattern for writing servers • A new Java class • A new technology • 在伺服器端執行的一種 Java 類別物件 • 接收 request • 回應 response
什麼是 Servlet Container ? • 又叫做 servlet engine • Web server 上的一個模組 • 載入 servlet 類別 • 執行 servlet 物件
三種 Servlet Container • Standalone 單獨存在 • Apache Tomcat • In-Process • The web server and the servlet container are different • the container runs within the address space of the main server as a plug-in • Out-of-process • the web server runs in one process while the servlet container runs in a separate process • To communicate with the servlet container, the web server uses a plug-in • 例如:Tomcat 的 mod_jk
常見的 servlet Container Tomcat (Apache) Resin (Caucho Technology) JRun (Macromedia) WebLogic (BEA) WebSphere (IBM) WebLogic and WebSphere, are much more than just servlet containers. They also provide support for Enterprise JavaBeans (EJB), Java Message Service (JMS), and other J2EE technologies
Hello World Servlet 寫一個 Java Servlet,輸出 Hello World 訊息 使用 Apache Tomcat 使用 Eclipse 參考 http://courses.ywdeng.idv.tw/java/
The Deployment Descriptor 請察看專案目錄中的 WebContent/WEB-INF/web.xml
Servlet Container 和 Servlet API之間的關係 • Servlet specification provides a standard and a platform-independent framework for communication between servlets and their containers • Made up of a set of Java interfaces and classes • Servlet Application Programming Interfaces
Packages • javax.servlet • generic servlet interfaces and classes that are independent of any protocol • javax.servlet.http • provides the basic functionality required for HTTP servlets
The javax.servlet.Servlet interface public void service (ServletRequest, ServletResponse) throws ServletException, java.io.IOException;
The javax.servlet.GenericServlet class The GenericServlet class implements the Servlet interface. It is an abstract class that provides implementation for all the methods except the service() method of the Servlet interface. It also adds a few methods to support logging. We can extend this class and implement the service() method to write any kind of servlet.
The javax.servlet.ServletRequest interface The ServletRequest interface provides a generic view of the request that was sent by a client. It defines methods that extract information from the request.
The javax.servlet.ServletResponse interface The ServletResponse interface provides a generic way of sending responses. It defines methods that assist in sending a proper response to the client.
The javax.servlet.http.HttpServlet class protected void service (HttpServletRequest, HttpServletResponse) throws ServletException, java.io.IOException; HttpServlet is an abstract class that extends GenericServlet.
The javax.servlet.http.HttpServletRequest interface The HttpServletRequest interface extends ServletRequest and provides an HTTP-specific view of the request. It defines methods that extract information, such as HTTP headers and cookies, from the request.
The javax.servlet.http.HttpServletResponse interface The HttpServletResponse interface extends ServletResponse and provides an HTTP-specific way of sending responses. It defines methods that assist in setting information, such as HTTP headers and cookies, into the response.
The advantages of the Servlet API Flexibility Separation of responsibilities It’s Java Portability
Summary • servlets and servlet container provide extensions to a server’s functionality • Conceptually, a servlet is a piece of code that can be • Plugged into an existing server to extend the server functionality • Used to generate the desired output dynamically
Summary • For a servlet container, a servlet is • A Java class like any other normal Java class • A class that implements the javax.servlet.Servlet interface • For a web component developer, a servlet, or specifically an HTTP servlet, is a class that • Extends javax.servlet.http.HttpServlet • Resides in a servlet container (such as Tomcat or JRun) • Serves HTTP requests