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!

4

1 に答える 1

5

java.lang.InstantiationException

これは基本的に、単純なバニラ Java 用語で、次の構成要素を意味します。

import homework10.Reservation;

// ...

Reservation reservation = new Reservation(); 

失敗した。

これには多くの原因が考えられます。

  1. ランタイム クラスパスにクラスがありません。
  2. クラス定義が見つかりません。
  3. クラスは非公開です。
  4. クラスには、パブリックの既定のコンストラクターがありません。
  5. パブリック デフォルト コンストラクターのコードが例外をスローしました。

これまでに提供されたコードに基づいて、実行していると思われるコードを実行していると 100% 確信していると仮定すると、それは原因 #1 または #2 にすぎません。そのクラスはパブリックであり、基本的に何もしないパブリックのデフォルト コンストラクターがあります。したがって、#3、#4、および#5に傷が付く可能性があります。

考えられる原因 #1 を修正するには、クラス ファイルがパスの webapp deploy に存在することを確認します
/WEB-INF/classes/homework10/Reservation.class。考えられる原因 #2 を修正するには、パッケージ構造を維持しながら、クラスが正しい方法でコンパイルされていることも確認する必要があります。したがって、Eclipse のような IDE を使用していないが、コマンド プロンプトで低レベルをいじっている場合は、クラスのコンパイル中にパッケージを含めるようにする必要があります。


あなたが見つけた可能な解決策については、

ほとんどの人は、type="..." の代わりに class="..." を使用することを推奨しているようです。

そのとおりです。詳細については、次の回答に進んでください: javax.servlet.ServletException: bean [name] not found within scopeただし、これは明らかに問題を解決しなかったため、特定のケースの原因ではありません。

または import ステートメントを使用する

これはまったく意味がありません。それらの人々は scriptlets と混同しています。それらは可能な限り避けるべきです。また<jsp:useBean>、実際には、それは別の話です。いくつかのヒントについては、サーブレットの wiki ページも参照してください。

于 2013-07-24T19:24:33.460 に答える