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
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.
ReplyDeleteWeb developer