Servlet interface is implemented by all servlet directly or indirectly. We can write a servlet program by implementing Servlet Interface. This interface declares some methods those are implemented by servlet instance. All the methods declared in Servlet interface are following. All methods are called by servlet container.
void
destroy
()
called before the servlet is being taken out of service.ServletConfig
getServletConfig
()
Returns aServletConfig
object, which contains initialization and startup parameters for this servlet.String
getServletInfo
()
Returns information about the servlet, such as author, version, and copyright.void
init
(ServletConfig config)
called after the servlet is being placed into service.void
service
(ServletRequest req, ServletResponse res)
Called to allow the servlet to respond to a request.
Writing servlet program using Servlet Interface
import java.io.*;
import javax.servlet.*;
public class MyServlet implements Servlet {
public void init(ServletConfig config) throws ServletException{
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest req, ServletResponse res) throws ServletException,IOException {
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.write("Hello, This program is written by extending Servlet Interface");
pw.close();
}
public String getServletInfo() {
return null;
}
public void destroy(){
}
}
import javax.servlet.*;
public class MyServlet implements Servlet {
public void init(ServletConfig config) throws ServletException{
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest req, ServletResponse res) throws ServletException,IOException {
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.write("Hello, This program is written by extending Servlet Interface");
pw.close();
}
public String getServletInfo() {
return null;
}
public void destroy(){
}
}
Comments