Saturday, 18 May 2013

Servlet Interface

Servlet Interface provides common behavior to all the servlet  Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests and to destroy the servlet and 2 non-life cycle methods.

Methods of servlet Interface: There are 5 methods in servlet interface. The init, service and destroy are the life cycle method these are invoked by the web container.
  • public void init(ServletConfig config) initializes the servlet. It is the life cycle method invoked by the web container only once.
  • public void service(ServletRequest request, ServletResponse response) provides response for the incoming request. It is invoked at each request by the web container.
  • public void destroy() is invoked only once and indicates that servlet is being destroyed.
  • public ServletCofig getServletConfig() returns the object of ServletCofig.
  • public String getServletInfo() returns information about servlet such as writer, copyright,version etc.
Example:

 import java.io.*;
import javax.servlet.*;
public class First implements Servlet{
ServletConfig config=null;

public void init(ServletConfig config){
this.config=config;
System.out.println("servlet is initialized");
}

public void service(ServletRequest req, ServletResponse res)throws IOException, ServletException{
 res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html>");<body>");
out.print("<b>Hai</b>");
out.print("</html></body>");
}

public void destroy(){ 
System.out.println("Servlet is destroyed");
}

pulbic ServletCofig getServletConfig(){ 
return config;
}

public String getServletInfo(){
return "copyrith @ futureimpact"
}


2 comments:

Unknown said...

The post was able to express what it wants to convey to the readers. It has been a very effective approach which resulted to a profitable output for all who have been fortunate enough to come across it!
skinny fiber diet

Unknown said...

I high appreciate this post. It’s hard to find the good from the bad sometimes but I think you’ve nailed it! would you mind updating your blog with more information?
selling a house fast

Post a Comment