The servlet class created in web applications usually extend the javax.servlet.HttpServlet class. The HttpServlet extends the javax.servlet.GenericServlet. These classes contain the basic features that are required to run a web application.
Both GenericServlet and HttpServlet as options to create a new servlet. The HttpServlet extends the GenericServlet and is the Http protocol oriented version of the GenericServlet. It can service only Http requests.
GenericServlet defines a generic, protocol-independent servlet whereas HttpServlet Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site that uses the Http Protocol.
The difference is that a GenericServlet has no defined protocol (it is "generic"), while HttpServler uses the HTTP protocol. HttpServlet is a subclass of GenericServlet with the purpose of creating a servlet for a web site.
You can get the ServletContext instance in a servlet by using the getServletContext() method provided by the HttpServlet class, which is the base class for servlets. This method returns the ServletContext object associated with the servlet. For example: ServletContext context = getServletContext();
Servlet is a Server side component, a servlet is a small pluggable extension to the server and servlets are used to extend the functionality of the java enabled server.
A Servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web serversA Servlet is a Java-based server-side web technology.The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.PrintWriter; import java.io.IOException; public class OurFirstServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/HTML"); PrintWriter out = response.getWriter(); out.println("< HTML >"); out.println("< head >< title >Servlet Example " + " "); out.println("< body >"); out.println("Not Much code, but this is enough for a Servlet."); out.println(""); out.println(""); } } The above is a simple Servlet. It would display an almost blank HTML page that contains the message we put in "Not Much code, but this is enough for a Servlet."
javax.servlet.GenericServletSignature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.SerializableGenericServlet defines a generic, protocol-independent servlet.GenericServlet gives a blueprint and makes writing servlet easier.GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.GenericServlet implements the log method, declared in the ServletContext interface.To write a generic servlet, it is sufficient to override the abstract service method.javax.servlet.http.HttpServletSignature: public abstract class HttpServlet extends GenericServlet implements java.io.SerializableHttpServlet defines a HTTP protocol specific servlet.HttpServlet gives a blueprint for Http servlet and makes writing them easier.HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.
we are passing values from the HTML to the Servlet by using HTML controls. ex:name,password.......... In servlets we have one method for getting the values from HTML is getParameter() example: String s1=req.getParameter("T1"); String s2=req.getParameter("T2"); in above example T1,T2 are the names of the HTML controls, String is a class and req is the object of the HttpServlet.
Because, the creators of the servlet framework cannot give all the functionality that you might want in your application. So, if they make their class abstract, you the developer will be providing all the functionality you need and still stick to the framework standards defined by them.
Both the GenericServlet and the HttpServlet can be used to create Servlets for a j2ee application. The only difference is that the HttpServlet can be used only for applications that use the Http protocol and not any other protocol but the GenericServlet can be used for any protocol.