Why DocumentBuilderFactory cannot use?

My domtree program:

package project1;

import javax.servlet.;
import javax.servlet.http.
;
import java.io.;
import java.util.
;
import javax.xml.parsers.;
import org.w3c.dom.
;
import toolbox.*;

public class Servlet1 extends HttpServlet {

//Initialize global variables

public static String bianhao,xingming,fenfabumen,danrengongzuo,Year1,Month1,benxin,jishujintie;
public static Document doc;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType(“text/html;charset=GB2312”);
request.setCharacterEncoding(“GB2312”);
bianhao=request.getParameter(“bianhao”);
xingming=request.getParameter(“xingming”);
fenfabumen=request.getParameter(“fenfabumen”);
danrengongzuo=request.getParameter(“danrengongzuo”);
Year1=request.getParameter(“Year1”);
Month1=request.getParameter(“Month1”);
benxin=request.getParameter(“benxin”);
jishujintie=request.getParameter(“jishujintie”);
System.out.println(“iuaysdhflkjsahdflkjhsdaflksda”);

// ???group tree in ram
try{
javax.xml.parsers.DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.newDocument();

Element root=doc.createElement("???");
Element table=doc.createElement("??");

Element bianhao1=doc.createElement("??");
bianhao1.appendChild(doc.createTextNode(bianhao));
table.appendChild(bianhao1);

Element xingming1=doc.createElement("??");
xingming1.appendChild(doc.createTextNode(xingming));
table.appendChild(xingming1);

Element fenfabumen1=doc.createElement("???");
fenfabumen1.appendChild(doc.createTextNode(fenfabumen));
table.appendChild(fenfabumen1);

Element danrengongzuo1=doc.createElement("???");
xingming1.appendChild(doc.createTextNode(danrengongzuo));
table.appendChild(danrengongzuo1);

Element daochangriqi=doc.createElement("???");

Element Year11=doc.createElement("?");
Year11.appendChild(doc.createTextNode(Year1));
daochangriqi.appendChild(Year11);

Element Month11=doc.createElement("?");
Month11.appendChild(doc.createTextNode(Month1));
daochangriqi.appendChild(Month11);

table.appendChild(daochangriqi);

Element benxin1=doc.createElement("??");
benxin1.appendChild(doc.createTextNode(benxin));
table.appendChild(benxin1);

Element jishujintie1=doc.createElement("??");
jishujintie1.appendChild(doc.createTextNode(jishujintie));
table.appendChild(jishujintie1);

root.appendChild(table);
doc.appendChild(root);

//TaminoWrapper.Process(“http://localhost/tamino/sinocomm”,doc);
//DOMWrapper.writeToXML(doc);
}
catch(Exception ex)
{
System.out.println(“Error”);
}

/*PrintWriter out = response.getWriter();
out.println("");
out.println(“gettree”);
out.println("");
out.println(“

The servlet has received a post.This is the reply.

”);
out.println(bianhao+"
"+xingming+"
"+fenfabumen
+"
"+danrengongzuo+"
"+Year1+"
"+Month1);
out.println(""); */
}


public void destroy() {
}
}


Error:
type Exception reportmessage Internal Server Errordescription The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.exception javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)


root cause java.lang.NoSuchMethodError
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(DocumentBuilderFactoryImpl.java:82)
at project1.Servlet1.doGet(Servlet1.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)



Why?If two packages have the same class,how can I choose it?

:smiley: :smiley: :smiley:

I am a Java NewComer.

I am a chinese beginner!I want every high hand and expert can help me!Thanks a lot! :smiley: :smiley: :smiley: :smiley:

I am a Java NewComer.

First, I think you are asking why your exception comes from org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
when you actually imported javax.xml.parsers.*

The answer is that javax.xml.parsers.DocumentBuilderFactory is an abstract class that purely defines the API for a generic DocumentBuilderFactory class.

The abstract class’s newInstance() method obtains an instance of a DocumentBuilderFactory implementation based on these factors:


  • Use the javax.xml.parsers.DocumentBuilderFactory system property.
  • Use the JAVA_HOME(the parent directory where jdk is installed)/lib/jaxp.properties for a property file that contains the name of the implementation class keyed on the same value as the system property defined above.
  • Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.parsers.DocumentBuilderFactory in jars available to the runtime.
  • Platform default DocumentBuilderFactory instance.

So, the type of DocumentBuilder you actually get depends on all of these factors.
In your case, probably the most relevant factor is the third one - the loader will look at all of the available jar files for a META-INF/services/javax.xml.parsers.DocumentBuilderFactory. In the case of xerces.jar (for example) this file exists and contains “org.apache.xerces.jaxp.DocumentBuilderFactoryImpl”. So using xerces, when you ask for javax.xml.parsers.DocumentBuilder you get an instance of org.apache.jaxp.DocumentBuilderFactoryImpl.

The only question this leaves is "Why doesn’t it work?"

What jar files do you have in your servlet’s classpath? They might be in /tomcat/application/WEB-INF/lib.

HTH