Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu.

Similar presentations


Presentation on theme: "Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu."— Presentation transcript:

1 Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu

2 Introduction Java technology isn't just for programming applets which run on the client side in web browsers, or for writing Internet applications. The Servlet API brings the power of Java to your servers,too. Java technology isn't just for programming applets which run on the client side in web browsers, or for writing Internet applications. The Servlet API brings the power of Java to your servers,too. Java TM Servlet technology provides web developers with a simple, consistent mechanism for extending the functionality of a web server and for accessing existing business systems. A servlet can almost be thought of as an applet that runs on the server side -- without a face.

3 What is a Servlet? Servlets are protocol- and platform- independent server side components, which dynamically extend Java enabled servers. They provide a general framework for services built using the request-response paradigm.Servlets are protocol- and platform- independent server side components, which dynamically extend Java enabled servers. They provide a general framework for services built using the request-response paradigm. Servlets run inside servers. They are Java application components which are downloaded, on demand.Servlets run inside servers. They are Java application components which are downloaded, on demand.

4 – –Clients talking to Java Servlets in servers. Clients may range in complexity from simple HTML forms to sophisticated Java applets. Servlets will frequently use some kind of persistent storage, such as files or a database.

5 Servlet OOP Architecture Servlet is an instance of a Java class which implements the javax.servlet.Servlet interface (provided by the JSDK). Most servlets do this indirectly, by extending javax.servlet.http.HttpServlet or j avax.servlet.http.GenericServlet, two of javax.servlet.Servlet's standard implementations.

6 Hierarchy : Servlet Servlet HttpServlet|GenericServlet HttpServlet|GenericServlet MyServlet

7 Ways to use Servlet A simple way: A simple way: Servlet can process data which was POSTed over HTTPS using an HTML FORM, say, an order-entry, Servlet can process data which was POSTed over HTTPS using an HTML FORM, say, an order-entry, and applying the business logic used to update a company's order database.

8 A simple servlet

9 Some other uses: Since servlets handle multiple requests concurrently, the requests can be synchronized with each other to support collaborative applications such as on-line conferencing.Since servlets handle multiple requests concurrently, the requests can be synchronized with each other to support collaborative applications such as on-line conferencing. One could define a community of active agents, which share work among each other. The code for each agent would be loaded as a servlet, and the agents would pass data to each other.One could define a community of active agents, which share work among each other. The code for each agent would be loaded as a servlet, and the agents would pass data to each other. One servlet could forward requests other servers. This technique can balance load among several servers which mirror the same content.One servlet could forward requests other servers. This technique can balance load among several servers which mirror the same content.

10 What does Servlet look like? Servlets implement the Servlet interface, usually by extending either the generic or an HTTP-specific implementation. The simplest possible servlet defines a single method, service:Servlets implement the Servlet interface, usually by extending either the generic or an HTTP-specific implementation. The simplest possible servlet defines a single method, service: import java.servlet.*;import java.servlet.*; public class MyServlet extends GenericServlet {public class MyServlet extends GenericServlet { public void service (ServletRequest request, public void service (ServletRequest request, ServletResponse response ServletResponse response ) throws ServletException, IOException ) throws ServletException, IOException {... {... } }

11 (cont’d) (cont’d) The service method is provided with Request and Response parameters. These encapsulate the data sent by the client, and allowing servlets to report status including errors.The service method is provided with Request and Response parameters. These encapsulate the data sent by the client, and allowing servlets to report status including errors. Servlets normally retrieve most of their parameters through an input stream, and send their responses using an output stream:Servlets normally retrieve most of their parameters through an input stream, and send their responses using an output stream: ServletInputStream in = request.getInputStream (); ServletOutputStream out = response.getOutputStream (); These input and output streams may be used with data in whatever format is appropriate. For example, an applet and servlet might exchange data using object serialization; HTML, and numerous image formats, may also be appropriate data formats.These input and output streams may be used with data in whatever format is appropriate. For example, an applet and servlet might exchange data using object serialization; HTML, and numerous image formats, may also be appropriate data formats.

12 Primary Servlet methods After being loaded, three methods are involved in the lifecycle of a servlet: Servlets are activated by the server through an init call. To provide implementation of this call is to perform potentially costly (usually, I/O intensive) setup only once, rather than once per request.Servlets are activated by the server through an init call. To provide implementation of this call is to perform potentially costly (usually, I/O intensive) setup only once, rather than once per request. After initialization, servlets handle many requests. Each client request generates one service upcall. These requests may be concurrent; this allows servlets to coordinate activities among many clients.After initialization, servlets handle many requests. Each client request generates one service upcall. These requests may be concurrent; this allows servlets to coordinate activities among many clients. Requests are processed until the servlet is explicitly shut down by the web server, by calling the destroy method. The servlet's class may then become eligible for garbage collection.Requests are processed until the servlet is explicitly shut down by the web server, by calling the destroy method. The servlet's class may then become eligible for garbage collection.

13 HTML-Aware Servlets Many servlets will directly generate HTML formatted text, use Java formatted output classes such as java.io.PrintWriter. There is no need to use scripting languages to dynamically modify or generate HTML pages.Many servlets will directly generate HTML formatted text, use Java formatted output classes such as java.io.PrintWriter. There is no need to use scripting languages to dynamically modify or generate HTML pages.

14 HTTP-Specific Servlets Servlets which are being used with the HTTP protocol may support any HTTP method, including GET, POST, HEAD, and more.Servlets which are being used with the HTTP protocol may support any HTTP method, including GET, POST, HEAD, and more. They may redirect requests to other locations, and send HTTP-specific error messages.They may redirect requests to other locations, and send HTTP-specific error messages. They can get access to parameters which were passed through standard HTML forms.They can get access to parameters which were passed through standard HTML forms.

15 Examples: String method = request.getMethod (); // e.g. POSTString method = request.getMethod (); // e.g. POST String name = request.getParameter ("name")String name = request.getParameter ("name") String phone = request.getParameter ("phone");String phone = request.getParameter ("phone"); String card = request.getParameter ("creditcard");String card = request.getParameter ("creditcard");

16 Performance Feature Servlets do not require creation of a new process for each request. In most environments, many servlets run in parallel within the same process as the server. When used in such environments with HTTP, servlets provide compelling performance advantages over both the CGI approach and the Fast-CGI approach.Servlets do not require creation of a new process for each request. In most environments, many servlets run in parallel within the same process as the server. When used in such environments with HTTP, servlets provide compelling performance advantages over both the CGI approach and the Fast-CGI approach.

17 Performance (cont’d)

18 Security Features Servlets have the Java advantage: memory access violations and strong typing violations are not possibleServlets have the Java advantage: memory access violations and strong typing violations are not possible Strong security policy support. All Java environments provide a Security Manager which can be used to control whether actions such as network or file access are to be permitted.Strong security policy support. All Java environments provide a Security Manager which can be used to control whether actions such as network or file access are to be permitted.

19 End


Download ppt "Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu."

Similar presentations


Ads by Google