Servlets are simple Java classes which extend the capabilities of the server that host the application using the request-response programming model.
All servlets must implement the Servlet interface which defines the servlet life cycle methods.
Lifecycle of a servlet
First of all the servlet class is loaded by the web container.
Container calls the init () method of servlet and must be called before servlet can service any request.
The service () method determines the kind of request being made and forwards it to an appropriate method to handle it.
Finally container calls destroy () method which indicates end of lifecycle of servlet. It takes servlet out of service
init () and destroy method are called only once in a servlet’s lifecycle.
The GenericServlet class can be extended by the servlet when it wants to provide any generic service.
The HTTPServlet class provides the methods for the HTTP related operations like get (doGet method), post (doPost method).
Usually the output of a servlet is in form of html.
The common methods of a servlet are:
init:
signature - public void init (ServletConfig sc) throws ServletException{}
usual semantic - The first line of this method is super.init (this); this makes a call to the super class’ init method. This call must be done through every servlet’s init method.
service:
signature - public void service (ServletRequest req, ServletResponse resp) throws ServletException, IOException{}
usual semantic - This method is used when we want to fulfill the request sent by client.
destroy:
signature- public void destroy(){}
usual semantic- This function contains the logic to free up the held up resources when the servlet is going to be destroyed, i.e. its going to be out of scope.
The common service methods provided by HTTPServlet class for the four HTTP commands are:
doGet:
signature - public void doGet (HTTPServletRequest req,HTTPServletResponse resp){}
usual semantic - This method is invoked when the servlet encounters the HTTP “GET” command.
doPost:
signature- public void doPost (HTTPServletRequest req,HTTPServletResponse resp){}
usual semantic- This method is invoked when the servlet encounters the HTTP “Post” command.
Apart from these methods there are also other methods namely doPut and the doDelete method.
Servlet Container
A servlet is managed by a servlet container.
The servlet container together with the web server provides the HTTP interface to the world.
It is the servlet container who calls service () method and passes an instance of ServletRequest and ServletResponse. Depending on this method service calls doGet () or doPost ().
Servlet container can get details about the remote user is and can generate appropriate response. It is responsible for loading and instantiating the servlets and calling init().
Following is an example of a simple genric servlet class:
import javax.servlet.*;
import java.io.*;
//the list of other files to be imported goes here.
public class Myservlet extends Servlet{
// declaration of variables goes here.
public void init() throws ServletException{
// the code that has to be executed when the Servlet initializes goes here.
}
public void Service(ServletRequest req, ServletRespoinse) throws ServletException, IOException{
// The code that needs to be executed when a request comes to this servlet comes here.
}
public void destroy(){
// The code that needs to be executed when the servlet is about to go out of scope comes here. Usually all the resources that the servlet used is freed from here.
}
//similarly other member functions of the servlet apart from these mandatory methods comes here.
}
4 comments:
hehe.. i didnt read it.. but it seems as if u r very clever! :P :P :P
Hey Great Job Surya ...!!!
kuch naya likh..
hey correct the spelling in syntax , it is ServletResponse resq , and plz JSP , JDBC , JavaBeans ke baare mein bhi likh .. thoda dhyan yaha bhi diya kar ..!!
Post a Comment