Java servlets
Servlets are protocol and platform independent server-side software components, written in Java. They run inside a Java enabled server or application server, such as the WebSphere Application Server. Servlets are loaded and executed within the Java Virtual Machine (JVM) of the Web server or application server, in much the same way that applets are loaded and executed within the JVM of the Web client. Since servlets run inside the servers, however, they do not need a graphical user interface (GUI). In this sense, servlets are also faceless objects.
Servlets more closely resemble Common Gateway Interface (CGI) scripts or programs than applets in terms of functionality. As in CGI programs, servlets can respond to user events from an HTML request, and then dynamically construct an HTML response that is sent back to the client.
Servlet process flow
Servlets implement a common request/response paradigm for the handling of the messaging between the client and the server. The Java Servlet API defines a standard interface for the handling of these request and response messages between the client and server.
1. The client sends a request to the server.
2. The server sends the request information to the servlet.
3. The servlet builds a response and passes it to the server. That response is dynamically built, and the content of the response usually depends on the client’s request. External resources may also be used.
4. The server sends the response back to the client.
High-level client-to-servlet process flow
Servlets are powerful tools for implementing complex business application logic. Written in Java, servlets have access to the full set of Java API’s, such as JDBC for accessing enterprise databases.
As mentioned above, servlets are similar to CGI in that they can produce dynamic Web content. Servlets, however, have the following advantages over traditional CGI programs:
❑Portability and platform independence: Servlets are written in Java, making them portable across platforms and across different Web servers, because the Java Servlet API defines a standard interface between a servlet and a Web server.
❑Persistence and performance: A servlet is loaded once by a Web server, and invoked for each client request. This means that the servlet can maintain system resources, like a database connection, between requests. Servlets don’t incur the overhead of instantiating a new servlet with each request. CGI processes typically must be loaded with each invocation.
❑Java based: Because servlets are written in Java, they inherit all the benefits of the Java language, including a strong typed system, object-orientation, and modularity, to name a few.
The Java Servlet API
The Java Servlet API is a set of Java classes which define a standard interface between a Web client and a Web servlet. Client requests are made to the Web server, which then invokes the servlet to service the request through this interface. The Java Servlet API is a Standard Java Extension API, meaning that it is not part of the core Java framework, but rather, is available as an add-on set of packages.
The API is composed of two packages:
❑javax.servlet
❑javax.servlet.http
The javax.servlet package contains classes to support generic protocol-independent servlets. This means that servlets can be used for many protocols, for example, HTTP and FTP. The javax.servlet.http package extends the functionality of the base package to include specific support for the HTTP protocol.
The Servlet interface class is the central abstraction of the Java Servlet API. This class defines the methods which servlets must implement, including a service() method for the handling of requests. The GenericServlet class implements this interface, and defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, we will use an even
more specialized class of GenericServlet called HttpServlet.HttpServlet provides additional methods for the processing of HTTP requests such as GET (doGet method) and POST (doPost method). Although our servlets may implement a service method, in most cases we will implement the HTTP specific request handling methods of doGet and doPost.
The servlet life cycle
A client of a servlet-based application does not usually communicate directly with a servlet, but requests the servlet’s services through a Web server or application server that invokes the servlet through the Java Servlet API. The server’s role is to manage the loading and initialization of the servlet, the servicing of the request, and the unloading or destroying of the servlet. This is generally provided by a servlet manager function of the application server.Typically, there is one instance of a particular servlet object at a time in the
Web servers’ environment. This is the underlying principle to the persistence of the servlet. The Web server is responsible for handling the initialization of this servlet when the servlet is first loaded into the environment, where it remains active (or persistent) for the life of the servlet.Each client request to the servlet is handled via a new thread against the original instance object. The Web server is responsible for creating the new threads to handle the requests. The Web server is also responsible for the unloading or reloading of the servlets. This might happen when the Web application is brought down, or the underlying class file for the servlet changes, depending on the underlying implementation of the server.
❑Servlet1 is initially loaded by the Web application server. Instance variables are initialized, and remain active (persistent) for the life of the servlet.
❑Two Web browser clients have requested the services of Servlet1. A handler thread is spawned by the server to handle each request. Each thread has access to the originally loaded instance variables that were initialized when the servlet was loaded.
❑Each thread handles its own requests, and responses are sent back to the calling client.
basic client-to-servlet interaction
The life cycle of a servlet is expressed in the Java Servlet API in the init, service (doGet or doPost), and destroy methods of the Servlet interface. We will discuss the functions of these methods in more detail and the objects that they manipulate.
Life-cycle of an individual servlet.
The WebSphere administrator can set an application and its servlets to be unavailable for service. In such cases, the application and servlets remain unavailable until the administrator changes them to available.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment