This is abstract class that is used to define protocol independent servlet. It implements the
Servlet and ServletConfig interfaces. You can create servlet easily by overriding services method only. public voiddestroy(): as defined beforepublicStringgetInitParameter(Stringname): returns a String containing initial parameter associated by specified name or null if not available.publicEnumerationgetInitParameterNames(): return all names of initial parameters as Enumeration.publicServletConfiggetServletConfig(): Returns this servlet'sServletConfigobject contains servlet configuration.publicServletContextgetServletContext():Returns a reference to theServletContextobject in which this servlet is running.publicStringgetServletInfo(): Returns information about the servlet, such as author, version, and copyright.publicStringgetServletName():Returns the name of this servlet instance.public voidinit():defined alreadypublic voidinit(ServletConfigconfig): called by server and server will pass the ServletConfig object’s reference for the servlet.public voidlog(Stringmsg): write messages to servlet log file.public voidlog(Stringmessage,Throwablet): writes messages to log file with a stack trace for a givenThrowableexception to log file.public abstract voidservice(ServletRequestreq,ServletResponseres): Called by the servlet container to allow the servlet to respond to a request.
Writing servlet program using GenericServlet class
import java.io.*; import javax.servlet.*; public class FirstServlet extends GenericServlet{ public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException{res.setContentType("text/html");PrintWriter pw=res.getWriter();pw.write("Hello Servlet");}}
Comments