Solved

Servlets and JAXP

Posted on 2001-08-06
7
420 Views
Last Modified: 2013-11-24
I need to do some XSLT processing in a servlet using the new TRaX functionality of JAXP 1.1.  Whenever I try to do a transform however, I get the following error:

java.lang.NoSuchMethodError
     at org.apache.xpath.DOM2Helper.getNamespaceOfNode(DOM2Helper.java:348)
     at org.apache.xml.utils.TreeWalker.startNode(TreeWalker.java:281)
     at org.apache.xml.utils.TreeWalker.traverse(TreeWalker.java:119)
     at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:320)

In order to be sure it had nothing to do with the JARs in my WEB-INF\lib directory I wrote a simple class that I could call either from the JDK or from the doGet method of a servlet:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.ParserConfigurationException;
 
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;  
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import java.io.*;
import com.lhs.pim.util.XSLTUtil;

public class Test
{
    public static void main(String[] args) {
     Test.TestXSLT();
    }
   
    public static void TestXSLT() {
     System.out.println("javax.xml.parsers.SAXParserFactory: " + System.getProperty("javax.xml.parsers.SAXParserFactory"));
     System.out.println("javax.xml.parsers.DocumentBuilderFactory : " + System.getProperty("javax.xml.parsers.DocumentBuilderFactory"));
     Document document = null;
     
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     try {
         File f = new File("C:\\ods\\bin\\test.xml");
         DocumentBuilder builder = factory.newDocumentBuilder();
         document = builder.parse(f);
     } catch (SAXException sxe) {
         // Error generated by this application
         // (or a parser-initialization error)
         Exception  x = sxe;
         if (sxe.getException() != null)
          x = sxe.getException();
         x.printStackTrace();
     } catch (ParserConfigurationException pce) {
         // Parser with specified options can't be built
         pce.printStackTrace();
     } catch (IOException ioe) {
         // I/O error
         ioe.printStackTrace();
     }

     System.out.println(XSLTUtil.toString(document));
    }
   
}

This works fine when run under JDK1.3 with the classpath containing all of the jars in my WEB-INF\lib directory, but it gives me the error show above when run during the doGet handler of a servlet.

Any ideas?
0
Comment
Question by:derekl
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:black
ID: 6357780
which appserver are you using? it's quite possible that the appserver parses an xml file before you do and therefore it loads up and xml parser, when you do your call your load up the one that's already instantiated and now your parser.
Try replacing the xml parser that your appserver comes with, with the xml parser that your application wants to use.
try printing out in your servlet:
    System.out.println("javax.xml.parsers.SAXParserFactory: " + System.getProperty("javax.xml.parsers.SAXParserFactory"));
    System.out.println("javax.xml.parsers.DocumentBuilderFactory : " + System.getProperty("javax.xml.parsers.DocumentBuilderFactory"));
0
 

Author Comment

by:derekl
ID: 6357839
I'm using Tomcat version 3.2.  When I run the servlet under Tomcat both of the System Properties I printed out came up as null.  It's interesting that parsing the test.xml file worked correctly but the xslt portion failed.
0
 
LVL 3

Accepted Solution

by:
black earned 250 total points
ID: 6357846
have you tried replacing the tomcat's xml parsers with yours? you can actually simply change the tomcat.bat file so your parsers appear before the tomcat parsers in the CLASSPATH and that way your parsers will be loaded first. I think it's better to remove the existing ones and use your ones that way there is not editing needed.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:wkang14
ID: 6359907
We have similar problem before when we are using xalan.  After we put xalan.jar & xerces.jar in the begin of classpath.  It solved the problem.
0
 

Author Comment

by:derekl
ID: 6360022
Yeah, I just moved xalan.jar, jaxp.jar, and crimson.jar to the %TOMCAT_HOME%\lib directory from my web apps WEB-INF\lib directory and it worked fine.  I am curious why it would not work with those three files in the WEB-INF\lib directory, because files in that directory are supposed to be added to the CLASSPATH.
0
 

Author Comment

by:derekl
ID: 6361394
Thanks, you got me on the right track.
0
 
LVL 3

Expert Comment

by:black
ID: 6362007
They are added to the classpath but after the entries in the %TOMCAT_HOME%\lib directory which means that when a search happens through the classpath for a suitable parser the tomcat parser's are found first and your parsers are not uses. that's why changing the order of the entries in the classpath fixes your problem.
Future versions of servlet engines and application servers are required to host the webapps on a separate classpath so that this issue does not occur. I'm not sure which servers do this yet.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now