Last week, you learned how to create, save, and compile a servlet. This week, you will learn more about working with servlets, such as how to code thread-save servlets. This article, the second of three parts, is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).
Developing Your Servlet Skills - Other skills for working with servlets (Page 2 of 4 )
Now that you have a basic understanding of how to code and test a servlet, you’re ready to learn some other skills for working with servlets. To start, you can learn about other methods that are available when you code servlets.
The methods of a servlet
Figure 5-6 presents some common methods of the HttpServlet class. When you code these methods, you need to understand that the servlet engine only creates one instance of a servlet. This usually occurs when the servlet engine starts or when the servlet is first requested. Then, each request for the servlet starts (or “spawns”) a thread that can access that one instance of the servlet.
When the servlet engine creates the instance of the servlet, it calls the init method. Since this method is only called once, you can override it in your servlet to supply any necessary initialization code. In the next figure, you’ll see an example of this.
After the servlet engine has created the one instance of the servlet, each request for that servlet spawns a thread that calls the service method of the servlet. This method checks the method that’s specified in the HTTP request and calls the appropriate doGet or doPost method.
When you code servlets, you shouldn’t override the service method. Instead, you should override the appropriate doGet or doPost methods. To handle a request that uses the Get method, for example, you can override the doGet method. If, on the other hand, you want to handle a request that uses the Post method, you can override the doPost method. To handle both types of requests, you can override both of them and have one call the other as shown in figure 5-3.
If a servlet has been idle for some time or if the servlet engine is shut down, the servlet engine unloads the instances of the servlets that it has created. Before unloading a servlet, though, it calls the destroy method of the servlet. If you want to provide some cleanup code, such as writing a variable to a file or closing a database connection, you can override this method. However, the destroy method may not be called if the server crashes. As a result, you shouldn’t rely on it to execute any code that’s critical to your application.
Five common methods of a servlet
public void init() throws ServletException{}
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{}
public void destroy(){}
How the server handles a request for a servlet
Figure 5-6.The methods of a servlet
The life cycle of a servlet
A server loads and initializes the servlet by calling the init method.
The servlet handles each browser request by calling the service method. This method then calls another method to handle the specific HTTP request type.
The server removes the servlet by calling the destroy method. This occurs either when the servlet has been idle for some time or when the server is shutdown.
Description
All the methods shown above are located in the abstract HttpServlet class. This means you can override these methods in your own servlets. However, you shouldn’t need to override the service method. Rather, you should override a method like doGet or doPost to handle a specific HTTP request.