Chapter: Internet & World Wide Web HOW TO PROGRAM - Servlets: Bonus for Java™ Developers

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Servlet Overview and Architecture

In this section, we overview Java servlet technology. We discuss at a high level the servlet-related classes, methods and exceptions. The next several sections present live-code exam-ples in which we build multi-tier client–server systems using servlet and JDBC technology.

Servlet Overview and Architecture

 

In this section, we overview Java servlet technology. We discuss at a high level the servlet-related classes, methods and exceptions. The next several sections present live-code exam-ples in which we build multi-tier client–server systems using servlet and JDBC technology.

 

The Internet offers many protocols. The HTTP (Hypertext Transfer Protocol) that forms the basis of the World Wide Web uses URIs (Uniform Resource Identifiers— some- times called Universal Resource Locators or URLs) to locate resources on the Internet. Common URIs represent files or directories and can represent complex tasks such as data-base lookups and Internet searches. For more information on URL formats, visit

 

www.w3.org/Addressing

 

For more information on the HTTP protocol, visit

 

www.w3.org/Protocols/HTTP

 

For information on a variety of World Wide Web topics, visit

 

www.w3.org

 

JavaServer Pages technology is an extension of servlet technology. Normally, JSPs are used primarily when most of the content sent to the client is static text and markup, and only a small portion of the content is generated dynamically with Java code. Normally, servlets are used when a small portion of the content sent to the client is static text or markup. In fact, some servlets do not produce content. Rather, they perform a task on behalf of the client, then invoke other servlets or JSPs to provide a response. Note that in most cases servlet and JSP technologies are interchangeable. The server that executes a servlet often is referred to as the servlet container or servlet engine.

 

Servlets and JavaServer Pages have become so popular that they are now supported directly or with third-party plug-ins by most major Web servers and application servers, including the Netscape iPlanet Application Server, Microsoft’s Internet Information Server (IIS), the Apache HTTP Server, BEA’s WebLogic application server, IBM’s WebSphere application server, the World Wide Web Consortium’s Jigsaw Web server, and many more.

The servlets in this chapter demonstrate communication between clients and servers via the HTTP protocol. A client sends an HTTP request to the server or servlet container. The server or servlet container receives the request and directs it to be processed by the appropriate servlet. The servlet does its processing, which may include interacting with a database or other server-side components such as other servlets, JSPs or Enterprise Java-Beans.3 The servlet returns its results to the client—normally in the form of an HTML, XHTML or XML document to display in a browser, but other data formats, such as images and binary data, can be returned.

 

1. Interface Servlet and the Servlet Life Cycle

 

Architecturally, all servlets must implement the Servlet interface. As with many key ap-plet methods, the methods of interface Servlet are invoked automatically (by the server on which the servlet is installed, also known as the servlet container). This interface defines five methods described in Fig. 30.1.

 


 

A servlet’s life cycle begins when the servlet container loads the servlet into memory—normally, in response to the first request that the servlet receives. Before the servlet can handle that request, the servlet container invokes the servlet’s init method. After init completes execution, the servlet can respond to its first request. All requests are handled by a servlet’s service method, which receives the request, processes the request and sends a response to the client. During a servlet’s life cycle, method service is called once per request. Each new request typically results in a new thread of execution (created by the servlet container) in which method service executes. When the servlet container terminates the servlet, the servlet’s destroy method is called to release servlet resources.

 

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

 

The examples in this chapter all extend class HttpServlet, which defines enhanced processing capabilities for servlets that extend the functionality of a Web server. The key method in every servlet is service, which receives both a ServletRequest object and a ServletResponse object. 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 or character based. If problems occur during the execution of a servlet, either ServletExceptions or IOExceptions are thrown to indicate the problem.

 

2. HttpServlet Class

 

Web-based servlets typically extend class HttpServlet. Class HttpServlet over-rides method service to distinguish between the typical requests received from a client Web browser. The two most common HTTP request types (also known as request methods) are get and post. A get request gets (or retrieves) information from a server. Common uses of get requests are to retrieve an HTML document or an image. A post request posts (or sends) data to a server. Common uses of post requests are to send information to a server—such as authentication information, data from a form that obtains user input, infor-mation that the server uses to search the Internet or query a database, etc.

 

Class HttpServlet defines methods doGet and doPost to respond to get and post requests from a client, respectively. These methods are called by the service method, which is called when a request arrives at the server. Method service first deter-mines the request type, then calls the appropriate method for handling such a request. Other less common request types are beyond the scope of this book. Methods of class HttpS-ervlet that respond to the other request types are shown in Fig. 30.2. They all receive parameters of type HttpServletRequest and HttpServletResponse and return void. The methods of Fig. 30.2 are not frequently used. For more information on the HTTP protocol, visit

 

http://www.w3.org/Protocols/

 

Methods doGet and doPost receive as arguments an HttpServletRequest object and an HttpServletResponse object that enable interaction between the client and the server. 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 to the Web client. Interfaces HttpServletRequest and HttpServletResponse are discussed in the next two sections.

 

3. HttpServletRequest Interface

 

Every call to doGet or doPost for an HttpServlet receives an object that imple-ments interface HttpServletRequest. The Web server that executes the servlet cre-ates an HttpServletRequest object and passes this to the servlet’s service method (which, in turn, passes it to doGet or doPost). This object contains the request from the client. A variety of methods are provided to enable the servlet to process the client’s re-quest. Some of these methods are from interface ServletRequest—the interface that HttpServletRequest extends. A few key methods used in this chapter are presented in Fig. 30.3. You can view a complete list of HttpServletRequest methods online at

 

java.sun.com/j2ee/j2sdkee/techdocs/api/javax/servlet/http/

HttpServletRequest.html

 

or you can download and install Tomcat (discussed in Section 30.3.1) and view the docu-mentation on your local computer.

 

4. HttpServletResponse Interface

 

Every call to doGet or doPost for an HttpServlet receives an object that imple-ments interface HttpServletResponse. The Web server that executes the servlet cre-ates an HttpServletResponse object and passes it to the servlet’s service method (which, in turn, passes it to doGet or doPost). This object provides a variety of methods that enable the servlet to formulate the response to the client. Some of these methods are from interface ServletResponse—the interface that HttpServletResponse ex-tends. A few key methods used in this chapter are presented in Fig. 30.4. You can view a complete list of HttpServletResponse methods online at

 

java.sun.com/j2ee/j2sdkee/techdocs/api/javax/servlet/http/

HttpServletResponse.html

 

or you can download and install Tomcat (discussed in Section 30.3.1) and view the docu-mentation on your local computer.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.