Sunday, January 18, 2015

Methods used in Servlets

There are many methods that are used in creating servlets.


Init() Method

the following code shows how the init() method is used.
public void init (ServletConfig config) throws ServletException


The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.
The servlet container cannot place the servlet into service if the init method
  1. Throws a ServletException
  2. Does not return within a time period defined by the Web server

getServletConfig() Method

public Servletconfig getServletConfig()

Returns a Servletconfig object, which contains initialization and startup parameters for this servlet. The ServletConfig object returned is the one passed to the init method.

getservletinfo() Method

public java.lang.String getServletInfo()

Returns information about the servlet, such as author, version, and copyright.
The string that this method returns should be plain text and not markup of any kind (such as HTML, XML, etc.).

doGet() Method

The get method is used to send a request for example when typing in a url into a browser.Here the browser simply requests the contents of a particular URL. When the browser uses this method, the web server ends up calling the servlet's doGet() method.

doPost() Method

When data entered to a web form is sent to a particular URL, the the doPost() method is used.
A POST request is a request to post (to send) form data to a resource on the server. This is the case of of a form in a web page declared this way in html: <form method="POST">. In this case the size of the parameters can be much greater.


doHead() Method

The doHead() method handles requests sent using the HTTP HEAD method. Similar to the GET method, the HEAD method also sends requests to the server. The only difference between the GET and the HEAD methods is that the HEAD method returns the header of the response, which contains entries, such as Content-Type, Content-Length, and Last-Modified.


destroy() Method

public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.


No comments:

Post a Comment