Servlet和XQuery

时间:2014-08-22 15:15:35

标签: servlets exception-handling xquery saxon

我写了一个java类来在XML数据库上运行一个简单的XQuery(使用Saxon xqj)。该查询在此类中运行良好。但是我想从servlet访问这个类。问题是servlet没有找到java类,当我运行servlet时它得到java.lang.ClassNotFoundException: javax.xml.xquery.XQException。有什么想法吗?

Java类就像这样简单:

import java.util.Properties;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;
import javax.xml.xquery.XQSequence;
import javax.xml.namespace.QName;
import net.sf.saxon.xqj.SaxonXQDataSource;
import org.xml.sax.SAXException;

public class XMLClass {
public static String xmldata() throws XQException{
    XQConnection con;
    String output = null;
    final String sep = System.getProperty("line.separator");
    String fileName= "cd_book.xml";
        con = new SaxonXQDataSource().getConnection();
        System.out.println("Connected");
    String queryString = "declare variable $docName as xs:string external;"+sep +
   "for $x in doc($docName)/*" +
   " return $x";
   XQPreparedExpression expr = con.prepareExpression(queryString);
   expr.bindObject(new QName("docName"), fileName, null);
   XQResultSequence rs = expr.executeQuery();
   String result =rs.getSequenceAsString(new Properties());
   return result;

    }
   }

Servlet是:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.xquery.XQException;
public class ServletXML extends HttpServlet {
private static final long serialVersionUID = 1L;

public ServletXML() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

try {
        XMLClass.xmldata();
        } catch (XQException e) {
            e.printStackTrace()
 }

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

1 个答案:

答案 0 :(得分:0)

然后xml你的类可能会从你的类路径中丢失。你是如何编译和运行它的?

同样最好将两者放在一个或多个包中,如果它位于不同的包中,则从servlet中导入类。