While a servlet is better if the display of the output is more important. Of course both can be used.
Sun's Answer Book is a good example of dynamically generated web pages.
UH Manoa Math Dept
The sourse.