JasperException: The value for the useBean class attribute is invalid
org.apache.jasper.JasperException: /index.jsp(1,1) The value for the useBean class attribute com.b5 is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1272)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Generator.generate(Generator.java:3426)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:216)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Can anyone explain the cause and solution of this problem?
Solution 1:
The value for the useBean class attribute com.b5 is invalid.
So you have a
<jsp:useBean id="b5" class="com.b5" />
This exception is typical when the following happening "behind the scenes" fails:
com.b5 b5 = new com.b5();
Apart from the requirement that it should be placed inside a package (which you thus correctly did), the bean should itself be a public
class and have an (implicit) public
no-arg constructor. I.e.
package com;
public class b5 {
public b5() {
// Default constructor is optional when there are no other constructors.
}
}
Normally this constructor is already present, but this will be hidden whenever you add other constructors which take other arguments. You'll then need to add it yourself explicitly.
package com;
public class b5 {
public b5(String argument) {
// Non-default constructor.
}
public b5() {
// You need to explicitly add a default constructor.
}
}
Another possible cause is that the bean class cannot be found in the runtime classpath. If this is your own bean, then ensure that its class
file is located in /WEB-INF/classes/com/b5.class
. Also ensure that the full qualified name com.b5
is literally correct, it's case sensitive. You should then look a bit further in the stacktrace for the exact cause of the problem. Head to the root cause
or caused by
parts at the bottom of the trace.
That said (and unrelated to the actual problem), the classname b5
is a pretty poor choice. It should be a sensible name starting with uppercase, e.g. User
, Product
, Order
, etc. Also, using <jsp:useBean>
instead of a servlet-based controller is a poor practice. If you're new to servlets, start at out servlets wiki page.
Solution 2:
Try type instead of class
The difference as per the documentation:
class="package.class" type="package.class" Instantiates a bean from the class named in class and assigns the bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class. The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive. beanName="{package.class | }" type="package.class" Instantiates a bean from a class, a serialized template, or an expression that evaluates to a class or serialized template. When you use beanName, the bean is instantiated by the java.beans.Beans.instantiate method. The Beans.instantiate method checks whether the package and class you specify represents a class or a serialized template. If they represent a serialized template, Beans.instantiate reads the serialized form (which has a name like package.class.ser) using a class loader. The value of type can be the same as beanName, a superclass of beanName, or an interface implemented by beanName. The package and class names you use with both beanName and type are case sensitive.
Solution 3:
I just had the same problem. In addition to the above make sure that you remembered to set your class itself to "public"
Solution 4:
I had the same problem and as highlighted above, I forgot to add the default constructor as I had added an overridden one.
Basic java, the compiler adds a default no arg constructor into every class you create, except for when you explicitly add a constructor.