JasperException: The value for the useBean class attribute is invalid
Asked Answered
jsp
P

7

11
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?

Paquin answered 11/6, 2010 at 16:33 Comment(1)
could you post the code that is causing this error?Palette
G
18
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.

Glower answered 11/6, 2010 at 17:37 Comment(3)
Hi! I'm with the same problem, but my classes are not located inside WEB-INF folder. However, i don't want to change my classes folder location. Do you know any way to precompile the jsp files having a different location for classes dir?Photoflood
I just had this problem and for whatever reason today Netbeans just didn't put the class file in my /build/web/WEB-INF/classes/. I hadn't made any changes, and this code is used frequently. I made some random small changes, recompiled, and then it showed up.Eastman
@Glower Adding default constructor worked. But can you tell me why do I need to have it? Couldn't program work fine if we don't have it? In my case I never use no-args constructor, but looks like I must have it. Why is it so?Middling
E
7

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.
Elnoraelnore answered 30/11, 2012 at 7:32 Comment(0)
A
1

I just had the same problem. In addition to the above make sure that you remembered to set your class itself to "public"

Assam answered 22/6, 2011 at 19:45 Comment(0)
G
1

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.

Gipon answered 18/10, 2012 at 19:54 Comment(0)
E
1

instead of
<jsp:useBean id="b5" class="com.b5" />
use
<jsp:useBean id='b5'>
<jsp:attribute name="class" trim="true">com.b5</jsp:attribute>
</jsp:useBean>

also restart tomcat server after changes

Everywhere answered 22/11, 2012 at 11:39 Comment(0)
A
0

Your workspace have the class which needed in useBean, so while working it will not throw any error message. But when you deployed the application it deployed as per deploy.properties file.It might be possible that your class you used in useBean is not getting deployed. Check your deploy.properties file.Hope it will work!!

Angelo answered 9/5, 2017 at 10:28 Comment(0)
E
0

i had the same problem i was i importing my class User from a package p1 but i was getting this error :

The value for the useBean class attribute [p1.User] is invalid.

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: /authen2.jsp (line: [11], column: [1]) The value for the useBean class attribute [p1.User] is invalid.

all i did is change pageEncoding="ISO-8859-1" to pageEncoding="UTF-8" and it worked !

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="UTF-8"%>
   <%@page import="p1.User" %>

Changing the page encoding to UTF-8 might have fixed the problem by ensuring better handling of characters and byte sequences. Sometimes, character encoding issues can cause unexpected errors in code interpretation. Switching to UTF-8 possibly resolved an underlying parsing issue, enabling the class import to function properly.

it worked !

Epeirogeny answered 15/12, 2023 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.