Java Tutorial - Java Script : Developing Servlets

Java Tutorial - Java Script :

Developing Servlets

Java servlets are created and compiled just like any other Java class. After you install the servlet packages and add them to your computer’s Classpath, you can compile servlets with the JDK’s Java compiler or any other current compiler. Every servlet is a subclass of the HttpServlet class, which is part of the javax.servlet package. This class includes methods that represent the life cycle of a servlet and collect information from the web server running the servlet. A servlet’s life cycle methods function similarly to the life cycle methods of applets. The init(ServletConfig) method is called automatically when a web server first brings a servlet online to handle a user’s request. As mentioned earlier, one Java servlet can handle multiple requests from different web users. The init() method is called only once, when a servlet comes online. If a servlet is already online when another request to use the servlet is received, the init() method won’t be called again.
The init() method has one argument—ServletConfig, an interface in the javax.servlet package that contains methods to find out more about the environment in which a servlet is running. The destroy() method is called when a web server takes a servlet offline.
 Like the init() method, this is called only once, when all users have finished receiving information from the servlet. If this doesn’t take place in a specified amount of time, destroy() is called automatically, preventing a servlet from being hung up while it waits for information to be exchanged with a user. One of the main tasks of a servlet is to collect information from a web user and present something back in response. You can collect information from a user by using a form, which is a group of text boxes, radio buttons, text areas, and other input fields on a web page. Figure 21.1 shows a web form on a page loaded with the Mozilla web browser. The form displayed in Figure 21.1 contains two fields: a text area and a clickable Translate button. The HTML tags used to display this page are the following:
The form is contained within the <form> and </form> HTML tags. Each field on the form is represented by its own tags: textarea for the text area and input for the Translate button. The text area is given a name, “text”.
Each field on a form stores information that can be transmitted to a web server and then sent to a Java servlet. Web browsers communicate with servers by using Hypertext Transfer Protocol (HTTP). Form data can be sent to a server using two kinds of HTTP requests: get and post. When a web page calls a server using get or post, the name of the program that handles the request must be specified as a web address, also called a uniform resource locator (URL).
A get request affixes all data on a form to the end of a URL, as in this example:
A post request includes form data as a header sent separately from the URL. This is generally preferred, and it’s required when confidential information is being collected on the form. Also, some web servers and browsers do not support URLs longer than 255 characters, which limits the amount of information that can be sent in a get request. Java servlets handle both of these requests through methods inherited from the HttpServlet class:
doGet(HttpServletRequest, HttpServletResponse) and doPost(HttpServletRequest, HttpServletResponse).
These methods throw two kinds of exceptions: ServletException, which is part of the javax.servlet package, and IOException, an exception in the standard java.io package that involves input and output streams. The doGet() and doPost() methods have two arguments: an HttpServletRequest object and an HttpServletResponse object. One is called when a get request is used to execute the servlet, and the other is called with post. A common technique in Java servlet programming is to use one method to call the other, as in the following example: