Adv Java - [Servlet: Useful Methods]

♠ Posted by Unknown in at 07:50

Useful Methods of Servlet

 Servlets are simply Java classes that follow three basic rules:

  1. An HTTP Servlet must extend javax.servlet.http.HttpServlet (or extend an abstract class that does).
  2. The Servlet must implement at least one of the following metheods: doGet(), doPost(), doPut(), doDelete(), init(), destroy(), or service().
  3. The Servlet can optionally implement init(), destroy(), or getServletInfo().

service() :-

All HTTP network requests have several “methods” of invocation that indicate the intent of the request, such as GET, POST, PUT and DELETE. In practice, only GET and POST methods are used. When a Servlet container first routes to a Servlet, it invokes the service() method. By default, the service() methods will dispatch to doGet(), doPost(), or doDelete() based on the method information given in HTTP request header. You can override this default service() implementation to respond to all HTTP requests in an identical way, although you would usually just override doGet() or doPost().

doGet() :-

The GET method is the most common form of HTTP request. The doGet() method receives an HttpServletRequest and an HttpServletResponse object. And must throws java.io.IOException and ServletException.

doPost() :-

HTTP employs the POST method to transfer large amounts of HTML form data. The doPost() method is called much like doGet().The doPost() method receives an HttpServletRequest and an HttpServletResponse object. And must throws java.io.IOException and ServletException. If the Servlet’s service() method receives an HTTP POST request, the Servlet’s doPost() method is called. The input parameters are identical to doGet(). The doPost() method is sometimes chained to doGet().

init() and destroy() :-

The init() and destroy() methods identify the beginning and end of the Servlet Life Cycle. The init() method is called when a Servlet is being placed into service by the container, and the destroy()  method is called when the container takes the Servlet out of service. These methods are most often used to initialize and release resources, or to provide other life cycle-sensitive support functions.

getServletInfo() :-

The getServletInfo() method can be overridden to provide information such as the Servlet author, company, copyright, or version. This method returns an empty string by default and is normally called by other Servlets.


0 comments:

Post a Comment