1 / 32

Java Servlet

Java Servlet. Presented by Hsin-Yi Wu http://www.albany.edu/~hw8054 March 13, 2000. Agenda. What are Servlets? Comparison of CGI, Server Extension APIs, and Servlet Servlet Basics Support for Servlet. What are Servlets?.

hallam
Download Presentation

Java Servlet

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. Java Servlet Presented by Hsin-Yi Wu http://www.albany.edu/~hw8054 March 13, 2000

  2. Agenda • What are Servlets? • Comparison of CGI, Server Extension APIs, and Servlet • Servlet Basics • Support for Servlet

  3. What are Servlets? • Java servlets are a key component of server-side Java development. • A servlet is a small, pluggable extension to a server that enhances the server’s functionality.

  4. Examples of Web Applications • Create dynamic content for a web page • keyword search on a document archive • electronic storefront

  5. Comparison of CGI,Server Extension APIs, and Servlet

  6. CGI (Common Gateway Interface) one of the first techniques for creating dynamic content intented purpose: to define a standard method for a server to talk with external applications CGI

  7. CGI life cycle CGI-based Web Server Main Process Request for CGI 1 Child Process for CGI1 Request for CGI 2 Child Process for CGI2 Request for CGI 1 Child Process for CGI1

  8. Proprietary server extension APIsfor example: Netscape provides an internal API called NSAPI Microsoft provides ISAPI exist within the main process of a web server Run extremely fast and make full use of the server’s resources Server Extension APIs

  9. Server Extension APIs life cycle Web Server with Server Extension API Main Process Server Extension 1 Server Extension 2 Request for Server Extension 1 Request for Server Extension 2 Request for Server Extension 1

  10. difficult to develop and maintain pose significant security and reliability hazards by crashed server extension Server Extension APIs (continued)

  11. Java Servlets • similar to a proprietary server extension, except that it runs inside a Java Virtual Machine(JVM) on the server • safe and portable • handled by separate threads within the web server process • efficient and scalable • compromise between CGI and native-interface applications (ex: ISAPI/NSAPI)

  12. Servlet life cycle Java Servlet-based Web Server Main Process JVM Request for Servlet 1 Thread Request for Servlet 2 Thread Thread Request for Servlet 1 Servlet1 Servlet2

  13. Interface Response From the May 5, 1998 issue of PC Magazine

  14. Comparison of Dynamic Web Interfaces- CGI • Portability: Fair • Performance: Fair • Pros: Can use familiar programming environment. • Cons: Slow. • How it Works: The Web server executes the CGI application as a separate process. From the May 5, 1998 issue of PC Magazine

  15. Comparison of Dynamic Web Interfaces- NSAPI/ISAPI • Portability: Poor • Performance: Excellent • Pros: Very fast. • Cons: Native API. Programming bugs can crash the Web server. • How it Works: The application is part of the Web server address space. From the May 5, 1998 issue of PC Magazine

  16. Comparison of Dynamic Web Interfaces- Java Servlet • Portability: Good • Performance: Good • Pros: Portable. Uses the JDK environment. • Cons: New and unstable. • How it Works: The Web server use Java Virtual Machine with a servlet add-on to interpret the Java applets. From the May 5, 1998 issue of PC Magazine

  17. Support for Servlet

  18. Tools you need: • Java Development Kit (JDK) • Java Servlet Development Kit(JSDK) • include packages:javax.servletjavax.servlet.http • available from http://java.sun.com/products/servlet • Servlet Engine

  19. Servlet Engines • Used to test and deploy your servlets • How to choose a servlet engine you need? • Depends on the web server(s) you are running • three flavors of servlet engines: • standalone • add-on • embeddable

  20. Servlet Engines • Standalone servlet engines • built-in support for servlets • hard to keep on with the latest version of servlet • Add-on servlet engines • plug-in to an existing server • Embeddable servlet engines • lightweight servlet deployment platform that can be embedded in another application

  21. For a list of available servlet engines, see the official list at: Http://jserv.java.sun.com/products/java-server/servlets/environments.html

  22. Servlet Basics • Java servlets are the first standard extension to Java,including two packages: • javax.servlet • javax.servlet.http

  23. Servlet Interface Servlet interface { void init(ServletConfig sc) throws ServletException; void service(ServletRequest req, ServletResponse res); throws ServletException, IOException; void destroy(); }

  24. Servlet Interface Servlet interface { void init(ServletConfig sc) throws ServletException; void service(ServletRequest req, ServletResponseres); throws ServletException, IOException; void destroy(); }

  25. Servlet Framework

  26. A GenericServlet handling a request GenericServlet subclass Server request service( ) response

  27. An HTTP servlet handling GET and POST request HttpServlet subclass Server Get request doGet( ) response service( ) Post request doPost( ) response

  28. Servlet life cycle Java Servlet-based Web Server Main Process JVM Request for Servlet 1 Thread Request for Servlet 2 Thread Thread Request for Servlet 1 Init() Servlet1 service() Servlet2

  29. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); out.println("<html>"); out.println("<head><title>Hello World</title></head>"); out.println("<body>"); out.println("<h1>Hello World</h1>"); out.println("</body></html>"); } }

  30. Saying Hello with JSP (hello.jsp) <HTML> <HEAD><TITLE>Hello</TITLE></HEAD> <BODY> <H1> <% out.println(“Hello World”); %> </H1> </BODY> <HTML>

  31. For reference, see my web page http://www.albany.edu/~hw8054

More Related