A Servlet is a Java programming language class that resides on a Web server that accepts requests and generates responses. The most common type of Servlet is an HTTP Servlet, which is implemented by the javax.servlet.http.HttpServlet Java class. The HTTP Servlet accepts HTTP requests and generates HTTP responses.
Other than HTTP, Servlets can accept requests and generate responses over other different communication protocols also.
Being a server resource, A Servlet has access to other server resources: other Servlets, EJBs, JSPs, and databases. The purpose of a Servlet is to generate a dynamic response.
Other than HTTP, Servlets can accept requests and generate responses over other different communication protocols also.
Being a server resource, A Servlet has access to other server resources: other Servlets, EJBs, JSPs, and databases. The purpose of a Servlet is to generate a dynamic response.
Implementing a servlet:
All servlets must implement the Servlet interface. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.
The Servlet interface has five methods, whose signatures are as follows:
The Servlet interface has five methods, whose signatures are as follows:
• public void init(ServletConfigconfig) throws ServletException
• public void service(ServletRequest request, ServletResponse response) throwsServletException, java.io.IOException
• public void destroy()
• public ServletConfiggetServletConfig()
• public java.lang.StringgetServletInfo()
• public void service(ServletRequest request, ServletResponse response) throwsServletException, java.io.IOException
• public void destroy()
• public ServletConfiggetServletConfig()
• public java.lang.StringgetServletInfo()
The init, service, and destroy methods are the servlet's lifecycle methods. The init method is called only once during a servlet lifecycle.The init method must complete successfully before the servlet can receive any requests. A servlet programmer can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, this method is normally left blank.
Servlets implement the javax.servlet.Servlet interface. While servlet writers can develop servlets by implementing the interface directly,but this is usually not required. Because most servlets extend web servers that use the HTTP protocol to interact with clients, the most common way to develop servlets is by extending the javax.servlet.http.HttpServlet class.
public class SimpleHttpServlet extends HttpServlet {
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("<html> <body>GET response </body> <html>");
}
}
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("<html> <body>GET response </body> <html>");
}
}
In the above example the doGet() method of HttpServletclass has been overridden. However the HttpServlet class has many more methods you can override for each HTTP method (GET, POST etc.). Here is a list of the methods that can be overriden:
• doGet()
• doPost()
• doHead()
• doPut()
• doDelete()
• doOptions()
• doTrace()
• doPost()
• doHead()
• doPut()
• doDelete()
• doOptions()
• doTrace()