wood burning stoves 2.0*
The moose likes Servlets and the fly likes Unable to create a bean for class myclass? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to create a bean for class myclass?" Watch "Unable to create a bean for class myclass?" New topic
Author

Unable to create a bean for class myclass?

Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Hi, I am using Tomcat3.2.3 and JDK1.3 on WIndows NT
I have set the JAVA_HOME,TOMCAT_HOME,path and classpath variables.When I try to run the JSP files with no beans they run fine.But when I am trying to access the JSP file with myclass bean i get the erroe that unable to create bean for myclass.How do i solve this problem.
I placed the class files in TOMCAT_HOME\classes directory(since this is in the classpth of tomcat) and jsp files in TOMCAT_HOME\webapps\mydir\
Plzz help
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi ... try this
1. since mydir is your context folder ... create a WEB_INF directory directly under it.
2. under the WEB-INF directory create a classes forlder and place your bean package in this folder.
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
It would help if you showed the JSP code where you are trying to create the bean - anyway, I will take a guess based on other folks problems.
1. Your bean should be a member of a package, NOT the default pkg
2. You must place the class files according to the rules for servlets - note how the Tomcat examples are constructed. This is all related to how your web application context is defined like Samith said.
3. Your <jsp:useBean tag must give the complete package in the class= attribute.
4. You must also import the package in the <%@ page directive
Bill
------------------
author of:
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
1.Is it a must that the bean has to be a part of a Package?
2. Here are the complete details :
JAVA_HOME: C:\jdk1.3.1
TOMCAT_HOME : C:\TOMCAT\JAKART~1.3
when i start the TOMCAT this is what is shown :
Including all jars in C:\TOMCAT\JAKART~1.3\lib in your CLASSPATH.
Using CLASSPATH: C:\TOMCAT\JAKART~1.3\classes;C:\TOMCAT\JAKART~1.3\lib\ant.jar;C
:\TOMCAT\JAKART~1.3\lib\jasper.jar;C:\TOMCAT\JAKART~1.3\lib\jaxp.jar;C:\TOMCAT\J
AKART~1.3\lib\parser.jar;C:\TOMCAT\JAKART~1.3\lib\servlet.jar;C:\TOMCAT\JAKART~1
.3\lib\webserver.jar;C:\jdk1.3.1\lib\tools.jar
Hence, I placed all my class files in C:\TOMCAT\JAKART~1.3\Classes , JSP files are in C:\TOMCAT\JAKART~1.3\webapps\mydir
I call the login.html which in turn calls the ValidateUser1.jsp which is
<jsp:useBean id="loginManager" class="/Classes/LoginManager"
scope="application" />
<jsp:useBean id="credentials" class="/Classes/UserCredentials"
scope="session" />
<jsp:setProperty name="credentials" property="*"/>
<%! String nextPage; %>
<%
if (loginManager.login(credentials)) {
nextPage="MainMenu.jsp";
}
else if (loginManager.alreadyLoggedIn(credentials)) {
nextPage="DuplicateLogin1.jsp";
}
else {
nextPage="LoginFailure1.jsp";
}
%>
<jsp:forward page="<%= nextPage %>"/>
<%@ page errorPage="Exception.jsp" %>
LoginManager is in Classes dir as mentioned above.Exception.jsp is as follows:
<%@ page isErrorPage="true" %>
<html>
<head>
<title>FATAL ERROR</title>
</head>
<body bgcolor="#ffffff">
<center>
<br>
<br>
<h1>A FATAL ERROR HAS OCCURRED!!!</h1>
</center>
<br>
<b>
<h2>
<%= exception.getMessage() %>
</h2>
<center>
<br>
Report above message to System Administrator
</b>
</center>
</body>
</html>
So when i click on submit buton inlogin.html it does go to ValidateUser1.jsp and then shows the message :
A FATAL ERROR HAS OCCURRED!!!
Cannot create bean of class LoginManager
Report above message to System Administrator
So why does it not create a bean?Is it because its not in a package?If i do place the bean in a package say com.something then i would have to include the bean as com.something.LoginManager - right?But would I need to change the directory structure in my Classes dir too like -
Classes
- com
-something(which has all bean class files)
Please help.I would be much obliged and very thankful
TIA -Kameswari

Originally posted by William Brogden:
It would help if you showed the JSP code where you are trying to create the bean - anyway, I will take a guess based on other folks problems.
1. Your bean should be a member of a package, NOT the default pkg
2. You must place the class files according to the rules for servlets - note how the Tomcat examples are constructed. This is all related to how your web application context is defined like Samith said.
3. Your <jsp:useBean tag must give the complete package in the class= attribute.
4. You must also import the package in the <%@ page directive
Bill

Tom Ebert
Greenhorn

Joined: Jul 18, 2001
Posts: 3
Hi,
it is a very usefull convention the every bean (just like every java class) should be part of a package, but not a must. Anyhow
"<jsp:useBean id="credentials" class="/Classes/UserCredentials"
scope="session" />" is not are correct class reference because you try to reference to a kind of directory structure (/Classes..).
The quick fix would propably be: Change the jsp tag to
"<jsp:useBean id="credentials" class="UserCredentials"
scope="session" />"
But the correct way would be:
Put your bean in a personal package just like
"com.yourcompany.yourname.beans". You do not have to
change the directory structure of the classes directory but it would be a good style to put the class file "UserCredentials.class" into the directory Classes/com/....
The correct reference ist then:
"<jsp:useBean id="credentials" class="com.mycompany.myname.beans.UserCredentials"
scope="session" />".
Hope that helps
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
Exactly right on most of that Tom - but I think there is a very good reason for putting beans in a package. At least with Tomcat, it has to do with the way the class loader looks for classes, and something about class="MyClass" confuses it.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to create a bean for class myclass?