GenericServlet class



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  void destroy() : as defined before          
  • public String getInitParameter (String name) : returns a String containing initial parameter associated by specified name or null if not available.
  • public Enumeration getInitParameterNames() : return all names  of initial parameters as Enumeration.
  • public ServletConfig getServletConfig() : Returns this servlet's ServletConfig object contains servlet configuration.
  • public ServletContext getServletContext():  Returns a reference to the ServletContext object in which this servlet is running.
  • public String getServletInfo() : Returns information about the servlet, such as author, version, and copyright.
  • public String getServletName(): Returns the name of this servlet instance.
  • public void init():     defined already
  • public void init(ServletConfig config) : called by server and server will pass the ServletConfig object’s reference for the servlet.
  • public void log(String msg) :  write messages to servlet log file.
  • public void log(String message, Throwable t) :  writes messages to log file with a stack trace for a given Throwable exception to log file.
  • public abstract  void      service(ServletRequest req, ServletResponse res) : 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");
}
}

No comments:

Popular Posts