Constructing a Servlet

Your servlet must extend the HttpServlet class. The servlet should override the methods doGet(), doPost() and  init().

The init() method must call the HttpServlet.init() method and must also call RealWeb.init() to ensure that connections are handled correctly. RealWeb.init() also searches for the reality.properties file and loads it if found. The properties file is also loaded when creating a connection via DDA or RSC.

doGet() or doPost() is called each time the web server receives a request for a page that addresses the server.

doGet() is the normal entry point; doPost() being called when the form on a web page uses the post method. doPost() can normally be implemented by simply calling doGet() as the Servlet API does not differentiate between parameters passed by the two methods. Hence the standard reality servlet, and the example rwdemo servlet included in this guide, override init() to start the RealWeb connection timeout thread, doPost() to call doGet() and doGet() to do all of the work.

doGet() and doPost() each take two parameters: HttpServletRequest and HttpServletResponse. HttpServletRequest provides methods to access the URL, the parameters to the page, and any cookies. HttpServletResponse provides methods to get an output stream for the HTML, set response header parameters and create cookies. For full information on the methods supplied by HttpServletRequest and HttpServletResponse, refer to the JavaDocs included in the JDK.

Note that, because there is only one instance of a Servlet class, the web server normally starts a new thread for each incoming request. Hence multiple threads can use this one class at the same time. Any instance variables within the class will need careful thought with respect to multi-threading, as all threads will see the same variable. For more information, please refer to the chapter on multi-threading in Thinking in Java (see Recommended Reading).

See Also

Example Servlet Code
Description of the Example Servlet
Debugging