I’m trying some of the examples from the

http://java.sun.com/developer/technicalArticles/javaserverpages/JSP20/ site.

 

Trying the 1 below the error message I get:

Anyone have any ideas?  Thanks

 

HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: The absolute uri: http://jakarta.apache.org/tomcat/jsp2-example-taglib %>
 
<HEAD>
<TITLE>Functions</TITLE>
</HEAD>
 
<BODY>
 
<H3>Add Numbers</H3>
<P>
<FORM action= "" be resolved in either web.xml or the jar files deployed with this application
        org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
        org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
        org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:118)
        org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:316)
        org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:147)
        org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:418)
        org.apache.jasper.compiler.Parser.parseDirective(Parser.java:483)
        org.apache.jasper.compiler.Parser.parseElements(Parser.java:1539)
        org.apache.jasper.compiler.Parser.parse(Parser.java:126)
        org.apache.jasper.compiler.ParserController.doParse(ParserController.java:220)
        org.apache.jasper.compiler.ParserController.parse(ParserController.java:101)
        org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.

 

 

Figure 2: Handling forms

Developing and Using Functions

The _expression_ language allows you to define functions that can be invoked in an _expression_. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD).

To illustrate the use of functions, I use a simple example to add two numbers. First we write the Java code for the method to add two numbers. Code Sample 3 shows a static method that accepts two Strings, parses them to integers, and returns their sum.

Code Sample 3: Compute.java

package jsp2.examples.el;
 
import java.util.*;
 
public class Compute {
   public static int add(String x, String y) {
      int a = 0;
      int b = 0;
      try {
        a = Integer.parseInt(x);
        b = Integer.parseInt(y);
      }catch(Exception e) {} 
      return a + b;
   }
}



Once this is successfully compiled using javac, the next step is to map the function's signature in the tag library. Code Sample 4 shows how to map the add function to the class containing the implementation of the function and the signature of the function. I will tell you where to add this later.

Code Sample 4: Function Descriptor

    <function>
        <description>add x and y</description>
        <name>add</name>
        <function-class>jsp2.examples.el.Compute
            </function-class>
        <function-signature>int
            add(java.lang.String,java.lang.String)
                </function-signature>
    </function>



Now, we can write the JSP page that uses the function. Code Sample 5 shows a form with two fields. The user enters two numbers and when the "Add Numbers" button is clicked, the function is called to add the numbers. The result is displayed on the same page.

Code Sample 5: math.jsp

<%@ taglib prefix="my" 
    uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib %>
 
<HEAD>
<TITLE>Functions</TITLE>
</HEAD>
 
<BODY>
 
<H3>Add Numbers</H3>
<P>
<FORM action="" method="GET">
   X = <input type="text" name="x" value="${param["x"]}">
   <BR>
   Y = <input type="text" name="y" value="${param["y"]}">
   <input type="submit" value="Add Numbers">
</FORM>
 
<P>
The sum is: ${my:add(param["x"],param["y"])}
 
</BODY>
</HTML>



To run this example:

  1. Copy Compute.java and save it at: C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\classes\jsp2\examples\el
  2. Compile Compute.java using javac
  3. Edit the file C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\jsp2\jsp2-example-taglib.tld and append the snippet of code in Code Sample 4 after the last </function> in the file and before </taglib>
  4. Copy math.jsp to c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial
  5. Request the jsp file math.jsp from a web browser

If all goes well, you should see something similar to Figure 3.

 

Reply via email to