The servlet interface is designed to make creating Web applications an easy and intuitive process. This chapter explains the process of creating a servlet and presents the code for a simple servlet.
The servlet described in this section is included in the files HelloWorldExample.h and HelloWorldExample.cpp in the examples\servlet directory of the HydraExpress installation. The servlet itself is available at http://localhost:8090/examples/HelloWorldExample on a system running HydraExpress.
Creating and deploying a servlet involves the following steps:
Create a class that inherits from rwsf::HttpServlet. This class provides the basic framework for a C++ servlet (Section 3.2)
Define the servlet by using the RWSF_DEFINE_SERVLET macro within a single source file (Section 3.3)
Implement the request handlers (Section 3.4)
Compile the servlet into a shared library (Section 3.5)
Copy the shared library into the path that the system uses to load shared libraries (Section 3.6)
Add the servlet deployment descriptor to the web.xml file in the servlet context (Section 3.7)
If the servlet resides in a new context, configure any external Web servers to forward requests to the new context. Section 4.13 describes the process of reconfiguring the Web server.
Stop and restart the HydraExpress Agent (Section 3.8).
At Agent startup, the Agent instantiates the servlet, and then calls the init() method on the servlet. Each time a client request arrives, the Agent calls a handler method on the servlet. Before the Agent shuts down, it calls the destroy() method on the servlet.
A servlet acquires resources through the init() method and releases resources in the destroy() method. The Agent always calls init() before forwarding requests to the servlet and always calls destroy() when the servlet is removed from service. However, because the servlet is instantiated at Agent startup, the Agent may construct the servlet before an incoming request invokes it. Further, the Agent may take the servlet out of service long before the servlet is destroyed. A servlet can take advantage of this situation by deferring resource acquisition until the Agent calls init() and releasing resources as soon as the Agent calls destroy().
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo are registered trademarks of Rogue Wave Software, and HydraExpress is a trademark of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.