• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem implementing EL function example from HFS&J :page 389

 
sammi red
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am preparing for SCWCD with HeadFirst book. I am having trouble with example given in page # 389.
I am getting this exception
org.apache.jasper.JasperException: The class specified in the method signature in TLD for the function mine:rollIt cannot be found.

I am using tomcat 5.0.12
J2SDK 1.4.2

DiceRoller.java
---------------
public class DiceRoller{
public static int rollDice( ){
return (int)((Math.random()*6)+1);
}
}

myFunction.tld
---------------
<?xml version="1.0" encoding="ISO-8859-1" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-
jsptaglibrary_2_0.xsd" version="2.0">

<tlib-version>1.2</tlib-version>
<uri>DiceFunctions</uri>
<function>
<name>rollIt</name>
<function-class>DiceRoller</function-class>
<function-signature>int rollDice( )</function-signature>
</function>
</taglib>

TestBean.jsp
------------
<%@ taglib prefix="mine" uri="DiceFunctions"%>
<%@ page isELIgnored="false" %>
<html><body>
${mine:rollIt()}
</body>
</html>

Deployment structure:
--------------------

TOMCAT-HOME/webapps/ch8/TestBean.jsp
TOMCAT-HOME/webapps/ch8/WEB-INF/classes/DiceRoller.class
TOMCAT-HOME/webapps/ch8/WEB-INF/myfunction.tld
TOMCAT-HOME/webapps/ch8/WEB-INF/web.xml




can anyone explain where I am doing wrong? or this is a bug in tomcat 5.0.18?

Thanks in advance
 
Wasim Ayoubi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sammi

it seems you did not package your class
try to declare your class in a package.

package whatever;
public class DiceRoller{
public static int rollDice( ){
return (int)((Math.random()*6)+1);
}
}

and modify the myFunction.tld
<function>
<name>rollIt</name>
<function-class>whatever.DiceRoller</function-class>
<function-signature>int rollDice( )</function-signature>
</function>

and everything will be fine

I have read that as of JDK1.4, it is highly recommended that you do NOT place java classes in the default package.
 
sammi red
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks wasim,

But after moving it to package and changing .tld , i get same error again

thanks
 
Wasim Ayoubi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you relocate the DiceRoller class in its new package?

The new compiled class should be somewhere like this

/WEB-INF/classes/whatever/DiceRoller.class

if you did and still have the problem please post your deployment structure again.
 
Nithya Venkatraman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

Even I am getting the same error.. I just tried out that example.
I have the DiceRoller class in WEB-INF/classes/ch1/
And have given the function-class in tld as ch1.DiceRoller
But I get that error..

Please help
 
Wasim Ayoubi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I tried out the example using the NetBeans IDE 4.0 with its embeded Apache Tomcat Server 5.0.28 and everything was fine (JDK used is 1.5).

can you post the full stackTrace of the exception thrown.
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might sound stupid, but what happens when you run this in the same web app.

Step 1: (TestBean.jsp)
<html>
<body>
The JSP is working!
</body>
</html>

Step 2: (TestBean.jsp)
<%@ taglib prefix="mine" uri="DiceFunctions"%>
<html><body>
${mine:rollIt()}
</body>
</html>
 
sammi red
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

there is a bug in tomcat earlier versions, which gives error when u used EL functions in jsps(figured out searching tomcat5.0 bug database). I tried the example with tomcat 5.0.28 and it worked fine.

Note: If u put your classes in deault package you will get following exception( So use packages)
org.apache.jasper.JasperException: Unable to compile class for JSP

Generated servlet error:
C:\jakarta-tomcat-5.0.28\jakarta-tomcat-5.0.28\work\Catalina\localhost\ch08\org\apache\jsp\TestBean_jsp.java:13: cannot resolve symbol
symbol : class DiceRoller
location: class org.apache.jsp.TestBean_jsp
_jspx_fnmap_0= org.apache.jasper.runtime.ProtectedFunctionMapper.getMapForFunction("m:rollIt", DiceRoller.class, "rollDice", new Class[] {});
^
1 error

Good look
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic