问题
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!
回答1:
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:
- Class is missing in runtime classpath.
- Class definition cannot be found.
- Class is not public.
- Class does not have a public default constructor.
- 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.
来源:https://stackoverflow.com/questions/17842892/java-lang-instantiationexception-bean-name-not-found-within-scope