1 / 26

Understanding Java servlets

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

liza
Download Presentation

Understanding Java servlets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding Java servlets 鄧姚文 joseph.deng@gmail.com

  2. Outline What is a servlet? What is a servlet container? Hello World servlet The relationship between a servlet container and the servlet API

  3. 什麼是 Servlet? • A server-side entity • A new design pattern for writing servers • A new Java class • A new technology • 在伺服器端執行的一種 Java 類別物件 • 接收 request • 回應 response

  4. Client-Server 架構

  5. 什麼是 Servlet Container ? • 又叫做 servlet engine • Web server 上的一個模組 • 載入 servlet 類別 • 執行 servlet 物件

  6. 三種 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

  7. Standalone Servlet Container

  8. In-Process Servlet Container

  9. Out-of-Process Servlet Container

  10. 常見的 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

  11. Hello World Servlet 寫一個 Java Servlet,輸出 Hello World 訊息 使用 Apache Tomcat 使用 Eclipse 參考 http://courses.ywdeng.idv.tw/java/

  12. The Deployment Descriptor 請察看專案目錄中的 WebContent/WEB-INF/web.xml

  13. 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

  14. Servlet Container 和 Servlet API之間的關係

  15. 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

  16. The javax.servlet.Servlet interface public void service (ServletRequest, ServletResponse) throws ServletException, java.io.IOException;

  17. 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.

  18. 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.

  19. 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.

  20. The javax.servlet.http.HttpServlet class protected void service (HttpServletRequest, HttpServletResponse) throws ServletException, java.io.IOException; HttpServlet is an abstract class that extends GenericServlet.

  21. 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.

  22. 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.

  23. The advantages of the Servlet API Flexibility Separation of responsibilities It’s Java Portability

  24. 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

  25. 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

More Related