Jsp Practice : Don't User jsp:useBean on Session Scope Attributes

My first best practice is a low level detail.

It's common to things like the following:



instead of doing the following:

<% String myVariable = (String) request.getAttribute("myVariable"); %>

There are some good reasons to utilize jsp:useBean. In addition to being easier to read, it becomes a self documenting way to tell other programmers the list of attributes used in the jsp file.

There is one issue with this approach in that the default behavior is to create the bean (using the default constructor) if the attribute doesn't exist in the identified scope. This may not be a huge issue for request scoped variables, but it can be a problem for session scoped. Imagine doing this.




If myBigObject doesn't exist in the session, it will be constructed and stored there. If you're object requires a lot of memory, takes time to construct, or if its existence in the session triggers some business logic, then the declaration above will cause you big problems. Chances are you won't find the issue until you work on another page that uses myBigObject and are puzzled over how it got in the session in the first place.

So bottom line rule of thumb:
  • Use jsp:useAttribute for request scoped attributes
  • Do not us jsp:useAttribute for session or application scope attributes

1 comment:

  1. After looking into a number of the articles on your website, I honestly like your way of blogging. I added it to my bookmark website list and will be checking back in the near future. Take a look at my website as well and let me know what you think.
    Web developer

    ReplyDelete

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.