• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[jsp:useBean] retrieving multiple attribute of same name in different scope

 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I added two objects [bean] with the same attribute name "name" to request and context objects. I forwarded the request to a jsp. I tried to retrieve the property value using jsp:useBean for the scope "application" and "request". But the jsp compiler throws the exception.

Can anyone help me to fix this.


exception

org.apache.jasper.JasperException: /test.jsp(21,0) useBean: Duplicate bean name: {0}
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:512)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1117)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2166)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2216)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2222)
org.apache.jasper.compiler.Node$Root.accept(Node.java:457)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2166)
org.apache.jasper.compiler.Validator.validate(Validator.java:1484)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:167)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:296)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
test.Test.doPost(Test.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.




JSP CODE

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both beans are declared using the same id. Give them different ids :
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i added two attributes with same name "name" for both context and request in the servlet.



I thing the jsp:useBean id attribute must match the attribute name thats been added by the servlet. I this case i cant use differnet id name to retrive the added bean.
[ September 29, 2008: Message edited by: yogesh srinivasan ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the id must match the attribute name, but it must be unique within the translation unit. You have to give your attribute different names. I think it's bad practice and calls for problems to give the same name to attributes in different scopes.
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i'm able to retrieve the attribute of same name stored under different scope using the scriptlets and it was working fine , without any exception. Then how can i make it working using jsp:useBean [Retrieving attributes with same name stored under different scope ]

 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the jsp complier creates an object with the same name as that of the attribute here attibute "name" and the object is also "name" of type [test.Movie].
So this will cause an error if we tend to extract an another attribute of same name value but of different scope as the JSP complier tend to create two variables of same name.

I hope this solution make you go nuts
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this solution make you go nuts

I don't see any solution here I hope you understand what I meant by "the id must match the attribute name, but it must be unique within the translation unit". If you want to get attributes with the same name, you can use EL instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic