Servlet Programming

To simplify the development of servlets, SunMicroSystems has given a predifined classes. They are "GenericServlet" and "HttpServlet". The following figure shows the relationship between classes.


relationship between servlet classes.
relationship between servlet classes.



From the above figure we can understand that Servlet is an Interface which was implemented by GenericServlet class. And HttpServlet is the sub-class of GenericServlet class. Both HttpServlet and GenericServlet classes are abstract. 

People say that there are three ways to develop the servlets. But according to SunMicroSystems, it is recommended to develop a servlet based on HttpServlet class, so that we can remove the redundant code of init(), destroy(), getServletConfig(), getServletInfo(). The following is the servlet based on HttpServlet. 

httpServlet
servlet based on HttpServlet


When we observe the above program, it doesn't have a main() method. Generally servlets doesn't consists a main() method, because servlets are executed by server not by JVM. At the same time in the above program we can see System.out.println("we are in First Servlet"). Since servlet program is executed by server the message "we are in First Servlet" will be displayed in server, which we cannot see. So, in servlet if we would like to display a message, it should be placed in PrintWriter object. The following program shows you, how to use PrintWriter object.

program shows you, how to use PrintWriter object
program shows you, how to use PrintWriter object

     
                                              

No comments:

Post a Comment