A Servlet and Its Request/Response

In this section we will present the skeleton of a servlet, and also look at the objects that it uses to learn about a request and send a response. A servlet is just like any other Java program, but one that runs inside a web server engine, termed the “container.”

There are some configurations or conventions that tell the servlet container where your servlet is and what URL should invoke it. The servlet container will call your overriding methods when that URL is requested, and pass in parameters that convey all the information in the form sent from the client browser.

You create your servlet by extending one of the javax.servlet classes, and overriding one or more of the methods in it with your own code. The skeleton of an HTTP servlet looks like this:

public class MyServlet extends javax.servlet.http.HttpServlet {

    public void init() { /* code */ }
    public void doGet() { /* code */ }
    public void doPost() { /* code */ }
    public void destroy() { /* code */ }
}

This code is simplified slightly by leaving off the method parameters and the exceptions they can throw. They are shown a couple of paragraphs later. The init() method is called only once when the class is first loaded. You would use this for one-time initialization, such as opening a connection to a database. The destroy() method is also called only once when the servlet is unloaded. This method is used to free any remaining resources or do final housekeeping on shutting down. If you don't have any special startup or shutdown code, you don't need to override these methods.

The doGet() or doPost() methods are the ones that do the work of the servlet. Obviously, doGet() is called when the HTML form used a get method, while doPost() is invoked by a form with a post method.

There are other less important doSomething() methods, too, corresponding to the other things that an HTML form may do. For example, there is a doDelete() method that can be overridden for the less-common HTTP request DELETE. This request is rarely implemented because you generally don't want to empower users with the ability to delete files on the server. You might allow it on a server on your intranet. You can review other methods in the API docs that are part of the servlet kit.

All the “do” methods, like doGet() and doPost(), take the same parameters and have a void return value. E.g.

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws javax.servlet.ServletException, java.io.IOException;

You call methods on the HttpServletRequest argument to find out what the browser is asking for exactly. There's a method you call to get a PrintWriter from the HttpServletResponse, and you write your resulting plain text, HTML, image file, audio file, or Javascript using it. What you write gets sent back to the browser. It's as simple as that.

Note that the two routines are labelled as “protected,” meaning that they can only be called by routines in the same package or in a subclass. That makes sense. It is probably not meaningful for your servlet to be called other than to process an HTTP request. If it is meaningful to invoke your doPost() routine otherwise, then your system design probably needs some refactoring, perhaps to split out a chunk of the common code into a Java bean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.225