Jsp Practice: Use Objects As Request and Session Attributes

It's very tempting to set String and other simple data types (Date, Long, etc) as request or session attributes. It's easy to program and a method for passing information from your classes to view objects (e.g. Struts actions to jsp pages).

The problem is, this can become unruly very quickly. Some pages end up with a long list of jsp:useBean's making them long and hard to follow. It's hard to enforce naming conventions. As pages get upgraded, it's difficult to know what attributes have become obsolete. Finally, these attributes often get implemented with little or no documentation. Worse, if you have a chain of actions or multiple paths to a jsp view, it's hard to know what attributes are needed or are available.

There is a simple solution. Create value objects (VO's) and add these to the request (or session) contexts. Then, create a naming convention for these VO's. The VO classes can be used to document the definition and rules around these attributes and house any logic. If you're using Eclipse then the overhead for creating the VO's is very light. Create the VO, add your attributes, then let Eclipse generate the getters and setters.

Simple enough? Yes, but even my team at TripConnect has trouble keeping up with this standard. Why? That's for another blog...

No comments:

Post a Comment

Comments on this blog are moderated and we do not accept comments that have links to other websites.

Share

About Isaac Sacolick

Isaac Sacolick is President of StarCIO, a technology leadership company that guides organizations on building digital transformation core competencies. He is the author of Digital Trailblazer and the Amazon bestseller Driving Digital and speaks about agile planning, devops, data science, product management, and other digital transformation best practices. Sacolick is a recognized top social CIO, a digital transformation influencer, and has over 900 articles published at InfoWorld, CIO.com, his blog Social, Agile, and Transformation, and other sites. You can find him sharing new insights @NYIke on Twitter, his Driving Digital Standup YouTube channel, or during the Coffee with Digital Trailblazers.