java.lang.InstantiationException: bean [name] not found within scope

独自空忆成欢 提交于 2019-12-06 04:04:45

问题


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:

  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.



来源:https://stackoverflow.com/questions/17842892/java-lang-instantiationexception-bean-name-not-found-within-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!