Originally posted by Pradeep Bhat:
Hi geeks,
The id attribute of jsp:useBean accepts only static value i.e. no dynamic value. But surprisingly using servlets it is possible.
I can do
String dynamicKey = "mykey";
session.setAttribute(dynamicKey ,"myvalue");
Dont U think that Sun should also allow for dynamic value for id attribute ?
OR
Could there any reasons for disallowing this?
Eagerly waiting for responses.
Thanks in advance.
Yes, there are reasons for disallowing it. The code in your servlet isn't quite analogous. Keep in mind that <jsp:useBean> doesn't just create a scoped attribute; it also creates a scripting variable. Scripting variables are part of the code of the servlet that the JSP compiles to; therefore, they must be available at translation time. (Dynamic values, by contrast, are only available at runtime, otherwise known as request time.)
Scripting variables are, in general, somewhat cumbersome; JSTL avoids them.