GenericServlet class
GenericServlet class implements Servlet,ServletConfig and Serializable interfaces. It provides the implementaion of all the methods of these interfaces except the service method.
GenericServlet class can handle any type of request so it is protocol-independent.
You may create a generic servlet by inheriting the GenericServlet class and providing the implementation of the service method.
Methods of GenericServlet class
|
There are many methods in GenericServlet class. They are as follows:
|
Servlet Example by inheriting the GenericServlet class
File: First.java
import java.io.*;
import javax.servlet.*;
public class First 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>”);
} }
HttpServlet class
|
The HttpServlet class extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, doHead, doTrace etc. |
Methods of HttpServlet class
|
There are many methods in HttpServlet class. They are as follows:
|
DemoServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet 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
pw.println(“<html><body>”);
pw.println(“Welcome to servlet”);
pw.println(“</body></html>”);
pw.close();
}}