When should I use a JSP tag library and under what scenario must I use JavaBeans?

Ryan Breidenbach

As a general rule, I would divide JSP Tag libraries and JavaBeans as so:

  • JavaBeans are used to encapsulate the "model" of you application. They contain the data and business logic. From a Model 2 perspective - this is what the servlet is placing in the request/session for the JSP to display. The JavaBeans themselves should have no "knowledge" they are being used in a JSP. In fact, the mechanism in how they are displayed should be comletely transparent to the JavaBean class.
  • Tag libraries are used to remove Java code from the JSP. Some very common uses of them are to perform boolean and null tests and iterations. In fact, if you have Tag libraries that perform these functions, you can probably remove 80% of the scriplet code from our JSPs! However, it is better if the JSP Tags do not contain any display logic, only logic that help the JSP display the corrent information. Basically - they support the JSP to keep them as "clean" as possible.