1 / 97

Enhancing a Web Server with Servlets

Enhancing a Web Server with Servlets. Servlets. Here focus on both sides of a client-server relationship . The client requests that some action be performed and the server performs the action and responds to the client.

astin
Télécharger la présentation

Enhancing a Web Server with 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. Enhancing a Web Server with Servlets

  2. Servlets • Here focus on both sides of a client-server relationship. • The client requests that some action be performed and the server performs the action and responds to the client. • This request-response model of communication is the foundation for the highest-level view of networking in Java—servlets.

  3. Servlets • A common implementation of the request-response model is between World Wide Web browsers and World Wide Web servers. • When a user selects a Web site to browse through their browser (the client application), a request is sent to the appropriate Web server (the server application). • The server normally responds to the client by sending the appropriate HTML Web page.

  4. Servlets • A servlet extends the functionality of a server. The javax.servlet package and the javax.servlet.http package provide the classes and interfaces to define servlets.

  5. Servlets • Servlet technology today is primarily designed for use with the HTTP protocol of the Web, but servlets are being developed for other technologies. • Servlets are effective for developing Web-based solutions that help provide secure access to a Web site, that interact with databases on behalf of a client, ...

  6. Servlets • ... that dynamically generate custom HTML documents to be displayed by browsers and that maintain unique session information for each client.

  7. Servlets • Many developers feel that servlets are the right solution for database-intensive applications that communicate with so-called thin clients—applications that require minimal client-side support.

  8. Servlets • The server is responsible for the database access. Clients connect to the server using standard protocols available on all client platforms. • Thus, the logic code can be written once and reside on the server for access by clients.

  9. Servlets • Our servlet example will make use of JDBC (Java Database Connectivity) database facilities to build a multi-tier client-server application that access a database.

  10. Servlets • The Servlet APIs are now developed by the Apache group (www.apache.org). • Before you can program with servlets, you must download and install the Apache group's implementation of servlets called Tomcat. • You may download Tomcat at no charge from Sun Microsystems at the Web site: java.sun.com/products/jsp/tomcat

  11. Servlets • You may download Tomcat at no charge from Sun Microsystems at the Web site: java.sun.com/products/jsp/tomcat

  12. Servlets • After downloading Tomcat, install it on your system and carefully read the readme file supplied in the doc directory. • It explains how to set up Tomcat and discusses how to start the server that can be used to test servlets if you do not have a Web server that supports servlets.

  13. Servlets • To develop servlets, you also need to copy the servlet.jar file containing the servlet class files from the installation directory to your JDK extensions directory (the directory c:\jdk1.3\jre\lib\ext on Windows or the directory ~/jdk1.3/jre/lib/ext on UNIX).

  14. Overview of Servlet Technology • Servlets are the analog on the server side to applets on the client side. Servlets are normally executed as part of a Web server. • In fact, servlets have become so popular that they are now supported by most major Web servers, ...

  15. Overview of Servlet Technology • ... including the Netscape Web servers, Microsoft's Internet Information Server (IIS), the World Wide Web Consortium's Jigsaw Web server and the popular Apache Web server.

  16. Overview of Servlet Technology • The servlets in this chapter show the communication between clients and servers via the HTTP protocol of the World Wide Web. • A client sends an HTTP request to the server. The server receives the request and directs it to be processed by appropriate servlets.

  17. Overview of Servlet Technology • The servlets do their processing (which often includes interacting with a database), then return their results to the client—normally in the form of HTML documents to display in a browser, but other data formats, such as images and binary data, can be returned.

  18. The Servlet API

  19. The Servlet API • We discuss at a high level the servlet-related classes, methods and exceptions. Architecturally, all servlets must implement the Servlet interface. • The methods of interface Servlet are invoked automatically (by the server on which the servlet is installed).

  20. The Servlet API • The servlet packages define two abstract classes that implement the interface Servlet—class GenericServlet (from the package javax.servlet) and class HttpServlet (from the package javax.servlet.http).

  21. The Servlet API • These classes provide default implementations of all the Servlet methods. • Most servlets extend either GenericServlet or HttpServlet and override some or all of their methods with appropriate customized behaviors.

  22. Servlets Methods of Interface Servlet

  23. MethodDescription • void init( ServletConfig config ) • This method is automatically called once during a servlet's execution cycle to initialize the servlet. The ServletConfig argument is supplied automatically by the server that executes the servlet.

  24. MethodDescription • ServletConfig getServletConfig() • This method returns a reference to an object that implements interface ServletConfig. This object provides access to the servlet's configuration information such as initialization parameters and the servlet's ServletContext, which provides the servlet with access to its environment (i.e., the server in which the servlet is executing).

  25. MethodDescription • void service( ServletRequest request, ServletResponse response ) • This is the first method called on every servlet to respond to a client request.

  26. MethodDescription • String getServletInfo() • This method is defined by a servlet programmer to return a String containing servlet information such as the servlet's author and version.

  27. MethodDescription • void destroy() • This "cleanup" method is called when a servlet is terminated by the server on which it is executing. • This is a good method to use to deallocate a resource used by the servlet (such as an open file or an open database connection).

  28. The Servlet API • The example we present extends class HttpServlet, which defines enhanced processing capabilities for servlets that extend the functionality of a Web server. • The key method in every servlet is method service, which receives both a ServletRequest object and a ServletResponse object.

  29. The Servlet API • These objects provide access to input and output streams that allow the servlet to read data from the client and ... • ... send data to the client. These streams can be either byte-based streams or character-based streams.

  30. The Servlet API • If problems occur during the execution of a servlet, either ServletExceptions or IOExceptions are thrown to indicate the problem.

  31. HttpServlet Class

  32. HttpServlet Class • Web-based servlets typically extend the class HttpServlet. • This class HttpServlet overrides the method service to distinguish between the typical requests received from a client Web browser.

  33. HttpServlet Class • The two most common HTTP request types (also known as request methods) are get and post. • A get request gets (or retrieves) information from the server. • Common uses of get requests are to retrieve an HTML document or an image.

  34. HttpServlet Class • A post request posts (or sends) data to the server. • Common uses of post requests are to send the to Web-server, information from an HTML form in which the client enters data, ... • ... to send to the server, information so that it can search on the Internet, ... • ... or query a database for the client, ... • ... to send authentication information to the server, etc.

  35. HttpServlet Class • The class HttpServlet defines the methods doGet and doPost, which respond to get and post requests from a client,respectively. • These methods are called by the HttpServlet class's service method, which is called when a request arrives at the server. • The method service first determines the request type, then calls the appropriate method.

  36. HttpServlet Class • Methods doGet and doPost receive as arguments an HttpServletRequest object and an HttpServletResponse object that enable interaction between the client and the server.

  37. HttpServlet Class • The methods of HttpServletRequest make it easy to access the data supplied as part of the request. • The HttpServletResponse methods make it easy to return the servlet's results in HTML format to the Web client.

  38. HttpServlet Class • The interfaces HttpServletRequest and HttpServletResponse are discussed now.

  39. HttpServletRequest Interface Important methods of interface HttpServletRequestMethod Descriptions

  40. HttpServletRequest Interface • Every call to doGet or doPost for an HttpServlet receives an object that implements interface HttpServletRequest. • The Web server that executes the servlet creates an HttpServletRequest object and passes this to the servlet's service method (which, in turn, passes it to doGet or doPost).

  41. HttpServletRequest Interface • This object contains the request from the client. A variety of methods are provided to enable the servlet to process the client's request. Some of these methods are from interface ServletRequest—the interface that HttpServletRequest extends.

  42. HttpServletRequest Interface • String getParameter( String name ) • Returns the value associated with a parameter sent to the servlet as part of a GET or POST request. The name argument represents the parameter name.

  43. HttpServletRequest Interface • Enumeration getParameterNames() • Returns the names of all the parameters sent to the servlet as part of a POST request

  44. HttpServletRequest Interface • String[] getParameterValues( String name ) • Returns a String array containing the values for a specified servlet parameter.

  45. HttpServletRequest Interface • Cookie[] getCookies() • Returns an array of Cookie objects stored on the client by the server. Cookies can be used to uniquely identify clients to the servlet.

  46. HttpServletRequest Interface • HttpSession getSession( boolean create ) • Returns an HttpSession object associated with the client's current browsing session. • An HttpSession object can be created by this method (true argument) if an HttpSession object does not already exist for the client. • HttpSession objects can be used in similar ways to Cookies for uniquely identifying clients.

  47. HttpServletResponse Interface Important methods of interface HttpServletResponseMethod Descriptions

  48. HttpServletResponse Interface • Every call to doGet or doPost for an HttpServlet receives an object that implements interface HttpServletResponse. • The Web server that executes the servlet creates an HttpServletResponse object and passes this to the servlet's service method (which, in turn, passes it to doGet or doPost).

  49. HttpServletResponse Interface • This object contains the response to the client. A variety of methods are provided to enable the servlet to formulate the response to the client. Some of these methods are from interface ServletResponse—the interface that HttpServletResponse extends.

  50. HttpServletResponse Interface • A few key methods used in this interface are presented as follow:

More Related