A servlet is a special class that is instantiated by the web container to handle HTTP requests. Typically it delegates to JavaBeans or EJBs to perform business logic before transferring control to a JSP.
A JSP is a web page that includes some dynamic content (e.g. JSP scriptlets, JSP expressions, JSP tags) and is used to present a view to the user. At execution time it actually runs as a servlet (it is compiled into a servlet).
I think it is not good practice to put the business logic in the servlet. We shouldn't assume that our application will be only accessed by the web. It is always better to place the business logic in the Model(Plain Old Java Code). Servlet can be used as a controller between the Java coding(Model) and JSP (or) Html (View - Presentation)
Whether my statement is correct??
Joined: Sep 22, 2005
Yes, the Model View Controller (MVC) pattern is a widely accepted good practice for separating concerns in a J2EE web application. JavaBeans or POJOs (or EJBs) are used for the Model, JSPs for the View and Servlets for the Controller.
A servlet looks like Java with some statements that write HTML (maybe).
An JSP looks like HTML with some Java script (maybe).
A common model is for incoming GET and POST requests to hit a servlet which calls other Java classes that handle any inbound data and generate any outbound data. When all the Java work is done, the servlet forwards to a JSP that prepares the response page.
Under the covers the container compiles a JSP into a really ugly servlet, so the two can do most of the same things. But it's good to have a clear idea of what their roles are and how you want them to be different. Consider coding rules like no HTML in servlets and no Java in JSPs.
For more details, google and read up on MVC-2, front controller or Struts architecture.
Wow, a bunch of people said similar things while I was away reading my mail! [ September 27, 2005: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi