JavaBeans vs. Custom Tags
By: Emiley J in Java Beans Tutorials on 2007-10-06
Custom tags, also known as JSP tag extensions (because they extend the set of built-in JSP tags), provide a way of encapsulating reusable functionality on JSP pages. One of the major drawbacks of scripting environments such as JSP is that it's easy to quickly put together an application without thinking about how it will be maintained and grown in the future. For example, the ability to generate dynamic content by using Java code embedded in the page is a very powerful feature of the JSP specification. Custom tags allow such functionality to be encapsulated into reusable components. Custom tags provide a great way for the logic behind common and recurring tasks to be wrapped up in an easy-to-use package.
It is necessary to know when to use tags as opposed to JavaBeans for wrapping up reusable functionality. After all, JavaBeans are reusable components and the JSP specification provides a built-in mechanism for integrating and utilizing the features provided by JavaBeans. Although both technologies can be used to achieve the same goal, that of encapsulating and abstracting data away from the JSP page, there are significant differences between the two.
JavaBeans are good general-purpose objects that encapsulate state in a portable "bucket". We will continue to use these in our examples because they make great business objects. Tags are a web-specific technology. Tags are primarily for generating presentation elements, and as such they primarily encapsulate behavior. In addition, custom tags are aware of the environment in which they are running. For example, custom tags have access to the same implicit objects as the ones available when developing JSP pages: pageContext, request, response, session, and so on. JavaBeans, however, are components that can be reused within any Java environment; hence, they don't know about such JSP specifics. Therefore, custom tags are a much better choice for encapsulating reusable functionality that will be used on JSP pages. Keep in mind the following rules:
- Use JavaBeans for representing and storing information and state. An example is building JavaBeans to represent the business objects in your application.
- Use custom tags to represent and implement actions that occur on those JavaBeans, as well as logic related to the presentation of information. An example from JSTL is iterating over a collection of objects or conditional logic.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java Beans ) |
Latest Articles (in Java Beans) |
Comments