Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes What is the best practice of handling attribute names? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What is the best practice of handling attribute names?" Watch "What is the best practice of handling attribute names?" New topic
Author

What is the best practice of handling attribute names?

Marcin Mogiela
Greenhorn

Joined: Aug 30, 2007
Posts: 14
Hi

Scope attribute names in Servlets/Jsp are strings. Due to typing errors it is bad to use them as such.

It would be good to have an enum or an interface with string constants. But having such an interface it

becomes a dependency magnet, every servlet and jsp in the system depends on it and requires

recompilation on every attribute name change or attribute addition.

It there any best practice or pattern to handle scope attribute names?

Thanks,
Marcin


SCJP 5.0, SCWCD 1.4
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Marcin,

from my personal experience I'd suggest you to keep the "string" properties where they belong to. In my opinion it makes everything more difficult if create separate interfaces or classes to hold these constants.

Why not just create public static string constants in those classes where you need them? This is a common practice in many swing components which also use strings as property names for listeners and other components which depend on these names. So you have only one place where you really define the names and all other classes only have to rely on some public static final string constants.

Marco
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

In a modern web application, the largest consumer of scoped variable names is EL expressions. And since the EL does not natively have any way to access string constants, defining such constants for scoped variable names is problematic.

If you really really really want to do it that way, you can read this article in which I devise a way to expose constants to the EL, but I never use it for scoped variable names -- I think it creates too messy a level of indirection.

Personally, when it comes to scoped variable names, I just type carefully.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Sorry, I didn't think about EL expression. In this case my advice is obviously not the very best idea

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the best practice of handling attribute names?
 
Similar Threads
Doubt in HFSJ page-567
!!My JSP Doubts!!
prepoputlate form
how to forward the result of a business login in servlet to the jsp
JSP/form/servlet - session persistence ?