This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes jsp:useBean with Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:useBean with Abstract class" Watch "jsp:useBean with Abstract class" New topic
Author

jsp:useBean with Abstract class

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 01, 2008
Posts: 6
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

Joined: Dec 11, 2004
Posts: 13410

"hemasundar ch",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 10, 2002
Posts: 61066
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 08, 2007
Posts: 621
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

Joined: Jan 28, 2008
Posts: 5575

i tried it is not working.any update on this ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 20, 2006
Posts: 3344

Yes, exactly! There you are

Good that you made it work. Congrats :thumb:
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks a lot Muthu , let cheers
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is com.Bean a subclass of com.Bean1?
If not, I'm not sure why this is working for you.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ben Souther:
Is com.Bean a subclass of com.Bean1?


yes. sorry Ben i forget to past Bean code here. ;)
 
 
subject: jsp:useBean with Abstract class