Servlet API
- Interfaces in javax.servlet package
- Classes in javax.servlet package
- Interfaces in javax.servlet.http package
- Classes in javax.servlet.http package
The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet api.
The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.
The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
Interfaces in javax.servlet package
|
There are many interfaces in javax.servlet package. They are as follows:
|
Classes in javax.servlet package
|
There are many classes in javax.servlet package. They are as follows:
|
Interfaces in javax.servlet.http package
|
There are many interfaces in javax.servlet.http package. They are as follows:
|
Classes in javax.servlet.http package
|
There are many classes in javax.servlet.http package. They are as follows:
|
Servlet Interface
Servlet interface provides common behaviour to all the servlets.
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 methods of servlet. These are invoked by the web container.
|
Method |
Description |
|
public void init(ServletConfig config) |
initializes the servlet. It is the life cycle method of servlet and 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 ServletConfig getServletConfig() |
returns the object of ServletConfig. |
|
public String getServletInfo() |
returns information about servlet such as writer, copyright, version etc. |
Servlet Example by implementing Servlet interface
File: First.java
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>hello simple servlet</b>”);
out.print(“</body></html>”);
}
public void destroy(){System.out.println(“servlet is destroyed”);}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return “copyright 2013-2014”;}
}