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

Now I’m learning how to use jsp and beans, and I can’t understand what I have.

I am trying to create a bean as follows: ...

and I get the error:

java.lang.InstantiationException: bean booking not found within the area

I looked online and most people seem to recommend using class = "..." instead of type = "..." or using the import statement. I already do the first and tried the last ... any ideas?

This is a 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 top 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
source share
1 answer

java.lang.InstantiationException

This basically means simple expressions of vanilla java that the following construction

 import homework10.Reservation; // ... Reservation reservation = new Reservation(); 

failed.

There are many possible reasons for this:

  • The class is missing from the execution path.
  • Class definition could not be found.
  • The class is not public.
  • The class does not have a public default constructor.
  • The code in the public default constructor made an exception.

Based on the code provided so far, and assuming that you are 100% sure that using a code that you think works, could be cause # 1 or # 2. This class is publicly available and has an open default constructor that does virtually nothing. So # 3, # 4 and # 5 can be scratched.

To fix possible cause # 1, make sure the class file is present in the webapp deployment in the path
/WEB-INF/classes/homework10/Reservation.class . To fix possible reason # 2, you also need to make sure the class is compiled correctly while maintaining the package structure. That way, when you are not using an IDE, such as Eclipse, but you run a low level on the command line, you must make sure that you include the package when compiling the class.


Regarding the possible solutions you found,

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

It is right. To find out more, open this answer: javax.servlet.ServletException: bean [name] not found within scope However this is clearly not the reason in your particular since this did not seem to solve the problem.

or using the import statement

This does not make full sense. These people get confused with the scripts. They should be avoided to any degree. <jsp:useBean> too, actually, but that's a different story. See also our Servlets widget page for some tips.

+5
source

Source: https://habr.com/ru/post/1493278/


All Articles