Hi Simon:
I understand your meanings.1:The transform protocol is wrong.You are right!I test it in the weblogic6.There is more important information in the weblogic console.The web page display:
//////////////////////////////////////////browser////////////////////////////////////////////////
"Error 500--Internal Server Error
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.5.1 500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request."
/////////////////////////////////////////End/////////////////////////////////////////////////
And the console display error is:
///////////////////////////////////////////////////////////////////////////////////////////////
<2003-7-19 afternoon> <Error> <HTTP> <Servlet execution in servlet contex
t "WebAppServletContext(3385924,DefaultWebApp,/DefaultWebApp)" failed, java.net.
ProtocolException: Didn't meet stated Content-Length, wrote: '982' bytes instead
of stated: '0' bytes.
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '982' byte
s instead of stated: '0' bytes.
at weblogic.servlet.internal.ServletOutputStreamImpl.finish(ServletOutpu
tStreamImpl.java:413)
at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImp
l.java:974)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
pl.java:1964)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:137)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
////////////////////////////End weblogic exception////////////////////////////////////////
The xml page also can display after I refresh more than three times.But it display a text page instead of xml
pattern(Note:I don't use xsl this time).In the browser:
1 lyo Testing successful? 2003-22-53-12 CST 2 li How many charactors 2003-6-29 23-01-31 CST 2 li there are many people. 2003-6-29 23-02-17 CST
The result indicate that the protocol in my servlet(text/html) is incorrect.You says it shouldn't read the data to a xml file from the database.Other than read the data to memory and transform them using XSLT.
Now,I have two method and I don't know which is best.
first, I download the xalan2.0:
I correct my servlet and using xslt transform xml data to the client's browser.
////////////////////////////////////mainPage.java/////////servlet/////////////////////////////
public void doPost(HttpServletRequest req,HttpServletResponse resp){
resp.setContentType("text/html");
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection(url);
stm=conn.createStatement();
rs=stm.executeQuery(sql);
this.createXml();
rs.close();
stm.close();
conn.close();
this.transform(req,resp);
}
public void createXml(){
SAXBuilder build=new SAXBuilder();
Document doc=build.build(new FileInputStream("C:\\tomcat\\webapps\\ROOT\\xmlbook\\mysql.xml"));
Element root=doc.getRootElement();
List books=root.getChildren();
System.out.println("debug... ...");
ResultSetBuilder rsbuilder=new ResultSetBuilder(rs);
doc=rsbuilder.build();
XMLOutputter xmlout=new XMLOutputter("",true,"GBK");
xmlout.output(doc,new FileOutputStream("C:\\tomcat\\webapps\\ROOT\\xmlbook\\mysqlnew.xml"));
}
public void transform(HttpServletRequest re,HttpServletResponse res){
final String hostname="localhost"; //why?? private don't right?
final String filename="/xmlbook/mysqlnew.xml";
final String stylesheet="C:\\tomcat\\webapps\\ROOT\\xmlbook\\mysqlxsl.xsl";
final int portnumber=3030;
URL urlbook=new URL("http",hostname,portnumber,filename);
InputStream in=urlbook.openStream();
XSLTProcessor process=XSLTProcessorFactory.getProcessor();
process.process(new XSLTInputSource(in),
new XSLTInputSource(new FileInputStream(stylesheet)),
new XSLTResultTarget(res.getOutputStream()));
}
I delete all the exception for simple the code.The most important method is the transform method.I use it transform xml data to the client browser.But it remain has to read data to a xml file first and read it to client next.I come across a exception while I use this method.When I execute the servlet the Tomcat report:
//////////////////////////////error//////////////////////////////////////////////////
[Fatal Error] :1:1: Premature end of file.
javax.xml.transform.TransformerException: SAX Exception
//////////////////////////////////////////////////////////////////////////////////////////////
I debug it and the error happen when I execute the "process.process(new XSLTInputSource(in),"
The first method failed and I don't understand why?
The second:
I want to use a javabean to store all the data that read from the database.All the data are stored in memory as your say.
The procedure is:
1.Write a javabean and it has getter and setter method:
public class lyobean{
private String id;
private String name;
private String title;
... ..... .......
public void setId(String id){
this.id=id; }
public String getId(){
return this.id;}
....... ........ ......
2.Read the data to javabean from database:
while(is.next()){
lyobean lyo=new lyobean();
lyo.setId(rs.getString("id"));
lyo.setName(rs.getString("name"));
lyo.setTitle(rs.getTitle("title"));
........ .......... ...........
}
3.using the DOM to create a XML tree in memory and place all the javabean data to the xml:
Element product = document.createElement( "id" );
Element temp = document.createElement( "id" );
temp.appendChild( document.createTextNode( getId() ) );
product.appendChild( temp );
temp = document.createElement( "title" );
temp.appendChild( document.createTextNode( getTitle() )
product.appendChild( temp );
....... ......... .......... .......... ...........
4.Finally transform all the data that in memory to the client using "xalan"
(I write a xsl file before I transform it)
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
InputStream xslStream =getServletContext().getResourceAsStream("/bookstore.xsl" );
transform( messageDocument, xslStream, out );
private void transform( Document document,
InputStream xslStream, PrintWriter output )
{
try {
Source xmlSource = new DOMSource( document );
Source xslSource =new StreamSource( xslStream );
Result result = new StreamResult( output );
TransformerFactory transformerFactory =TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer( xslSourcetransformer.transform( xmlSource, result );
}
You think which is better?And I don't want to use DOM because it is more complex than JDOM.Any idea? :roll: