Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a bean from jsp file error

Status
Not open for further replies.

darine

Programmer
Jun 23, 2000
28
0
0
US
Hi all<br>I'm trying to call a bean from jsp file and i'm getting this error:(500)<br>&nbsp;Class jsp.StringBean not found.<br>here is my bean file:<br><br>public class StringBean<br>{<br> private String message=&quot;No message specified &quot;;<br> public String getMessage()<br> {<br> return(message);<br> <br> }<br> public void setMessage(String message)<br> {<br> this.message=message;<br> }<br><br><br>}<br><br>i put it in :<br>install_dir\webapps/web_inf/classes<br><br><br>also here is my jsp file:<br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;table border=5 align=&quot;CENTER&quot;&gt;<br>&lt;TR&gt;&lt;TH class=&quot;TITLE&quot;&gt;<br>using javabaens<br>&lt;/table&gt;<br><br>&lt;jsp:useBean id=&quot;stringBean&quot; class=&quot;StringBean&quot; /&gt;<br><br>&lt;OL&gt;<br>&lt;LI&gt; initial value (getproperty):<br>&nbsp;&lt;I&gt;&lt;jsp:getProperty name=&quot;stringBean&quot; property=&quot;message&quot; /&gt;&lt;/I&gt;<br>&nbsp;<br>&nbsp;&lt;LI&gt; initial value (jsp expression):<br>&nbsp;&lt;I&gt;&lt;%=stringBean.getMessage() %&gt;&lt;/I&gt;<br>&nbsp;<br>&nbsp;&lt;LI&gt; &lt;jsp:setProperty name=&quot;stringBean&quot; property=&quot;message&quot; value=&quot;best string bean:fortex&quot; /&gt;<br>&nbsp;<br>&nbsp;value after setting property with setProperty &quot;<br>&nbsp;&lt;I&gt;&lt;jsp:getProperty name=&quot;stringBean&quot; property=&quot;message&quot; /&gt;&lt;/I&gt;<br><br>&lt;LI&gt;&lt;% stringBean.setMessage(&quot;my favorite:kentucky wonder &quot;); %&gt;<br>&nbsp;value after setting property with scripltlet:<br>&nbsp;&lt;I&gt;&lt;%=stringBean.getMessage() %&gt;&lt;/I&gt;<br>&nbsp;<br>&nbsp;&lt;/OL&gt;<br>&nbsp;<br>&nbsp;&lt;/BODY&gt;<br>&nbsp;&lt;/html&gt;<br>i put it under:<br>install_directory/webapp/examples/jsp directory.<br><br>i will appreciate your help<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
Dear darine,<br><br>&gt; i put it in :<br><br>You put what in that directory? The .java source file? That won't work. The .class file must be in the class path for a bean to be used. You also need to import the class or package if there is one in the JSP page using the import directive.<br><br>Also I don't know why most of the examples found on the NET show the coding method that you are useing, i.e.:<br><br>&gt; &lt;jsp:useBean id=&quot;stringBean&quot; class=&quot;StringBean&quot; /&gt;<br>&gt; &lt;jsp:setProperty name=&quot;stringBean&quot; property=&quot;message&quot; value=&quot;best string bean:fortex&quot; /&gt;<br><br>etc.<br><br>I just create the beans for local use with 'new', I find it much simpler to code and easier to read, i.e.:<br><br>&lt;%@ page import=&quot;java.util.*, java.lang.*, myclasses.StringBean&quot; %&gt;<br>&lt;%<br>&nbsp;&nbsp;StringBean stringBean = new StringBean();<br>&nbsp;&nbsp;stringBean.setMessage(&quot;best string bean:fortex&quot;);<br>%&gt;<br>&lt;p&gt;&lt;%=stringBean.getMessage()%&gt;&lt;/p&gt;<br><br><br>NOTE: There is a Java Server Pages forum here at Tek-Tips.<br><br>-pete
 
Sorry i'm still confusing about it.<br>would you please send a steps to do it using Tomcat3.1 or jswdk.<br>and where should i put those files.<br>i will appreciate your help.
 
darine,<br><br>I am using jswdk 1.0.1<br><br>&gt; and where should i put those files<br><br>Like always when working with Java, in the 'classpath'. <br><br>The default installation of the jswdk should already have beans in this location:<br><br>/Examples/WEB-INF/jsp/beans/...<br><br>To start with you could just use that location until you feel comfortable enough with your knowledge level to make your own classpath entry and start your own folders.<br><br>Hope this helps<br>-pete
 
Hi Darine!!

If i understands yr question, the possible error is beans should be packaged.They
cannot be recognised independantly though you keep bean class file in the classpath
&quot;classes&quot;. The bean concept is like that.

Here is the solution..

package TestBeanPkg;
public class TestBean extends Canvas
{
.........
}

compile it as javac -d . TestBean.java

then the bean's class will be created in a package called TestBeanPkg

now, you can use in jsp file
<jsp:useBean id=&quot;testbean&quot; class=&quot;TestBeanPkg.TestBean&quot;/>
I hope this should work.
pl. let me know whether it solves yr problem or not.

--vyshni


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top