Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean with Abstract class

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i try to test index.jsp ...i got below exception
----------------------------------------
javax.servlet.ServletException: java.lang.InstantiationException: bean ins not found within scope
----------------------------------

index.jsp
------------------------------------
<jsp:useBean id="ins" type="com.Bean1" scope="page"/>
<form action="path/seetha.23">
<jsp:getProperty name="ins" property="value1"/>
<input type="text" name="user"/>
<input type="submit" value="submit"/>
<form>
------------------------------------------------

my Bean1.java

-----------------------------------
package com;

/**
*
* @author seetharaman.v
*/
abstract public class Bean1{

public Bean1(){
this.value1="***Super Class***";
}

private String value1;

public String getValue1(){
return value1;
}
public void setValue1(String value1){
this.value1=value1;
}

}
---------------------------------------------

please anyone explain me ?...one thing bean must be exist means what?

Thanks & Regards,
seetharaman.v
 
ch hemasundar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friend,
you are forcing the container to create object for an abstarct class. I mean your specified type attribute in <jsp:useBean> is com1.Bean1, which is actually is an abstract. You can't create an object for an abstract class. That's why you may be getting problem.
I think this may help you

regards
Hemasundar
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"hemasundar ch",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hemasundar ch:

you are forcing the container to create object for an abstarct class.


Not exactly.
Had seetharaman used the class attribute to point to his abstract class, then yes, the container would look for an instance of that class in the specified scope and then, if not found, try to instanciate one using that class's zero argument constructor. If the class could not be instanciated (which it couldn't if it's abstract) then an exception would have been thrown.

In this case, he's used the type attribute.
When this is done, the server will not try to create an object of that type or a suitable subtype. If it can't find an instance of that type in the specified scope, it will throw an exception.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:

If it can't find an instance of that type in the specified scope, it will throw an exception.


Thanks Ben , but i can not getting you so, exactly what i have to do to avoid this Exception.. please can you clarify?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that a bean of the specified type is established in the specified scope. Otherwise, you'll get the exception. If that's not what you are intending, what is?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Make sure that a bean of the specified type is established in the specified scope.


Sorry ben distrubing you again!!! ..please tell me how can iestablish specified type in the specified scope?... if possible show the code!!!


 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Bear says is,


Make sure that a bean of the specified type is established in the specified scope.


You have your bean set in the appropriate scope (page, request, session) wherever appropriate and from where you are picking it up. That means, set the scope in the useBean tag so that the JSP Engine would do the needful.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
set the scope in the useBean tag so that the JSP Engine would do the needful.



Hi muthu,

the thing is that i can not get you.so please can you past your code that establishing bean into the scope?...thats my question....!!!

please give me the code or tell how to make bean exist into scope already?


 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


i think what Raghavan mean to say is as below




i hope it helps you to some extent.


Dhwani:>Winning is not important but it is the only thing.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it is not working.any update on this ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i got it after long R&D.. sorry BEN ;)

i think in this way we have to put bean into the scope
--------------------------------------
<%
com.Bean b=new com.Bean();
request.setAttribute("ins",b);%> //or i put this in servlet
---------------------------------


<jsp:useBean id="ins" type="com.Bean1" scope="request"/>

<jsp:getProperty name="ins" property="value1"/>


now it is working perfectly ....

is it correct?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, exactly! There you are

Good that you made it work. Congrats :thumb:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Muthu , let cheers
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is com.Bean a subclass of com.Bean1?
If not, I'm not sure why this is working for you.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Is com.Bean a subclass of com.Bean1?


yes. sorry Ben i forget to past Bean code here. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic