What are different ways of writing servlet programs ? Write a sample Servlet program using any one way.
Writing Servlets Program
There are three ways to create the servlet
1. By implementing the Servlet interface
2. By inheriting the GenericServlet class
3. By inheriting the HttpServlet class
1. By implementing the Servlet interface/Servlet Interface
The Servlet interface provides common behavior to all the servlets. The 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, service the requests, and destroy the servlet, and 2 non-life cycle methods that are used to get servlet information and servlet configurations.
Example
import java.io.*;
import javax.servlet.*;
public class ServletInt 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 "Implementing Servlet Interface";}
}
Output
When we execute this program we will see the message “ Hello simple servlet” in web browser.
2. By inheriting the GenericServlet class/GenericServlet Class
GenericServlet class implements Servlet, ServletConfig and Serializable interfaces. It provides the implementation of all the methods of these interfaces except the service method. Generic Servlet class can handle any type of request so it is protocol-independent. We can create a generic servlet by inheriting the GenericServlet class and providing the implementation of the service method.
Example
import java.io.*;
import javax.servlet.*;
public class GenServlet extends Genericservlet {
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 Generic Servlet</b>");
out.print("</body></html>");
}
}
Output
When we execute this program we will see the message “Hello Generic Servlet" in the web browser.
3. By inheriting the HttpServlet class/HttpServlet Class
The HttpServlet class extends the Generic Servlet class and implements a Serializable interface. It provides http-specific methods such as doGet, doPost, etc. doGet() method handles the GET request. And doPost() handles the POST request. Get request is the default request.
Example
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class HttpServletExample extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html"); //setting the content type
Printwriter pw=res.getWriter();//get the stream to write the data
//writing html in the stream
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
pw.close();//closing the stream
}
}
Output
When we execute this program we will see the message “Welcome to Servlet” in the web browser.
Comments
Post a Comment