320 likes | 551 Views
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?.
E N D
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? • 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.
Examples of Web Applications • Create dynamic content for a web page • keyword search on a document archive • electronic storefront
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
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
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
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
difficult to develop and maintain pose significant security and reliability hazards by crashed server extension Server Extension APIs (continued)
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)
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
Interface Response From the May 5, 1998 issue of PC Magazine
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
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
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
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
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
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
For a list of available servlet engines, see the official list at: Http://jserv.java.sun.com/products/java-server/servlets/environments.html
Servlet Basics • Java servlets are the first standard extension to Java,including two packages: • javax.servlet • javax.servlet.http
Servlet Interface Servlet interface { void init(ServletConfig sc) throws ServletException; void service(ServletRequest req, ServletResponse res); throws ServletException, IOException; void destroy(); }
Servlet Interface Servlet interface { void init(ServletConfig sc) throws ServletException; void service(ServletRequest req, ServletResponseres); throws ServletException, IOException; void destroy(); }
A GenericServlet handling a request GenericServlet subclass Server request service( ) response
An HTTP servlet handling GET and POST request HttpServlet subclass Server Get request doGet( ) response service( ) Post request doPost( ) response
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
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>"); } }
Saying Hello with JSP (hello.jsp) <HTML> <HEAD><TITLE>Hello</TITLE></HEAD> <BODY> <H1> <% out.println(“Hello World”); %> </H1> </BODY> <HTML>
For reference, see my web page http://www.albany.edu/~hw8054