4

I am learning how to use jsp and beans now, and can't understand what the issue I'm having.

I am trying to create a bean like this: ...

and am getting the error:

java.lang.InstantiationException: bean reservation not found within scope

I have looked around online, and most people seem to recommend using class="..." instead of type="...", or using an import statement. I am already doing the former and tried the latter...any ideas?

This is the bean:

package homework10;

public class Reservation {

private int groupSize;
private String status;
private double cost;
private boolean triedAndFailed;

public Reservation(){      
}

public void setGroupSize(int gs)
{
    groupSize = gs;
}

public int getGroupSize()
{
    return groupSize;
}
public void setStatus(String str)
{
    this.status = str;
}

public String getStatus()
{
    return status;
}    

public void setCost(double cost)
{
    this.cost = cost;
}

public double getCost()
{
    return cost;
} 

public void setTriedAndFailed(boolean bool)
{
    this.triedAndFailed = bool;
}

public boolean isTriedAndFailed()
{
    return triedAndFailed;
}    

}

and this is the beginning of the jsp page:

<head>
    <!--<script type="text/javascript" src="functions8.js">
    </script>-->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BHC Calculator-HTML-validation + Servlet Version</title>
    <jsp:useBean id = "reservation" class = "homework10.Reservation" scope = "session" />
</head>

Thanks in advance!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jo.P
  • 1,139
  • 4
  • 15
  • 35
  • Type represents the interface or parent class of your Bean class,so it is recommended to use as Class attribute rather type – Phani Jul 24 '13 at 19:25
  • @Phani: No, you misunderstood it either. See the link in my answer. – BalusC Jul 24 '13 at 19:29
  • @BalusC Oh..Could you please point me to the sources of where it was mentioned it return exception if you give it as type? – Phani Jul 24 '13 at 19:38
  • @Phani: lot of JSP containers are open source like Tomcat. Just look yourself. – BalusC Jul 24 '13 at 19:41
  • @BalusC Finally, I found answer to my question.. Under the JSP 2.1 Specification for UseBean Tag semantics, it is mentioned "The type attribute should be used to specify a Java type that cannot be instantiated as a JavaBean (i.e. a Java type that is an abstract class, interface, or a class with no public no-args constructor).". Its the specification, not the implementation. :) – Phani Jul 24 '13 at 19:44

1 Answers1

5

java.lang.InstantiationException

This basically means in plain vanilla Java terms that the following construct

import homework10.Reservation;

// ...

Reservation reservation = new Reservation(); 

has failed.

There are many possible causes for this:

  1. Class is missing in runtime classpath.
  2. Class definition cannot be found.
  3. Class is not public.
  4. Class does not have a public default constructor.
  5. The code in the public default constructor threw an exception.

Based on the code provided so far, and assuming that you're 100% certain that you're running the code you think you're running, then that can only be cause #1 or #2. That class is public and has a public default constructor which does essentially nothing. So #3, #4 and #5 can be scratched.

To fix possible cause #1, make sure that the class file is present in the webapp deploy in the path
/WEB-INF/classes/homework10/Reservation.class. To fix possible cause #2, you should also make sure that the class is compiled the right way while preserving the package structure. So, when you're not using an IDE like Eclipse, but you're fiddling low level in command prompt, then you should make sure that you include the package while compiling the class.


As to the possible solutions you found,

and most people seem to recommend using class="..." instead of type="..."

That's correct. To learn more, head to this answer: javax.servlet.ServletException: bean [name] not found within scope However, this is clearly not the cause in your particular case as it apparently didn't solve the problem.

or using an import statement

This makes no utter sense. Those people are confusing with scriptlets. They should be avoided to all extent. The <jsp:useBean> also, actually, but that's a different story. See also our Servlets wiki page for some hints.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks! I just went to netbeans' "file" tab and do see that in the "web" folder is WEB_INF --> classes --> homework10 -->Reservation.class . So it seems like it might be #2, but I am using netbeans (not working through the command line at all...)... So I'm still puzzled. That which you mentioned, "assuming that you're 100% certain that you're running the code you think you're running", is intriguing. I am running it from the "run" button in netbeans...could something be off there? Thanks again – Jo.P Jul 24 '13 at 19:37
  • I don't do Netbeans, so I'm somewhat limited in giving detailed directions. At least, try to "clean" and "rebuild" the project, server, cache, etc. Even closing/reopening the project or restarting the whole IDE should make sure that there's nothing dirty in the build. That said, are you able to create a servlet and import/construct that `Reservations` class "the usual Java way"? If not and you got an exception over there as well, then *that* exception should in turn undoubtedly contain much more detail about the real problem cause and thus also give better clues about the solution. – BalusC Jul 24 '13 at 19:38
  • K, I worked on it, and in the process ran into some other issues, fixed them, and now it works! Thanks for your help--your answer was good and helpful, and your direction pushed me in the right direction! – Jo.P Jul 24 '13 at 19:54