|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
sending xml response from servletHello,
Can someone recommend me a set of libraries that allow a servlet receive/send XML documents from/to xforms? I have found several libraries, but I don't know which is more adapted for what I want to do. I'm using xforms in client side and a servlet for xindice (DB manager) calls in server side. The servlet gets the xml document from xforms and add it in the DB correctly (I used a Xindice web application example and the tip 'Xforms tip: Accepting XForms data in Java' for its construction), but I don't know how can I get a XML document from DB and put it in the response object of the servlet. The example of the tip works with strings, so following it for the response I would have to take the XML document of the DB, transform it into a string, and then send it. Cannot be the XML document sent as application/xml without transforming it into a string? Maybe questions are more java related than xforms, but they are working-with-xml related so I think that I can found help in this forum. Thanks Iñaki |
|
|
Re: sending xml response from servletHi Iñaki, I am no servlet or java guru by any stretch of the imagination, but this is what I did to build a simple servlet that got a xml document from xforms, tweaked a few text nodes in it (via the method changePassword()), and then sent back a xml response to replace the instance that was sent: public void doWork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String contentType = req.getContentType(); if(contentType.equalsIgnoreCase("text/xml") || contentType.equalsIgnoreCase("application/xml")) { InputStream inputStream = req.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(inputStream); if(doc != null) { boolean result = changePassword(doc); if (result == true) { resp.setContentType(contentType); // following code serializes dom to xml file OutputFormat of = new OutputFormat(doc); of.setIndenting(false); ServletOutputStream outputStream = resp.getOutputStream(); XMLSerializer serializer = new XMLSerializer(); serializer.setOutputFormat(of); serializer.setOutputByteStream(outputStream); DOMSerializer domSerializer = serializer.asDOMSerializer(); domSerializer.serialize(doc); outputStream.flush(); outputStream.close(); } } } catch (Exception e) { e.printStackTrace(); } } } Again, I don't know if this is the way a 'real' app designer would do it, but it worked for me in my little test scenario. Good luck, --Aaron Iñaki Salinas Bueno wrote: > Hello, > > Can someone recommend me a set of libraries that allow a servlet > receive/send XML documents from/to xforms? I have found several > libraries, but I don't know which is more adapted for what I want to do. > > I'm using xforms in client side and a servlet for xindice (DB manager) > calls in server side. > > The servlet gets the xml document from xforms and add it in the DB > correctly (I used a Xindice web application example and the tip 'Xforms > tip: Accepting XForms data in Java > <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' > for its construction), but I don't know how can I get a XML document > from DB and put it in the response object of the servlet. > > The example of the tip works with strings, so following it for the > response I would have to take the XML document of the DB, transform it > into a string, and then send it. Cannot be the XML document sent as > application/xml without transforming it into a string? > > Maybe questions are more java related than xforms, but they are > working-with-xml related so I think that I can found help in this forum. > > Thanks > Iñaki |
|
|
RE: sending xml response from servletHere are some hints, building on Aaron's implementation: 1. You might need to set the resulting content type to "application/xml; charset=UTF-8" instead of whatever the input was, because the output looks like it will be UTF-8 below. 2. There are some efficiency considerations here that are subtle and hard to find. See http://www.nabble.com/JAXP-performance-problems-t2279828.html Be careful of the textbook pattern DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(). Sun's factory implementation does really expensive calls (scans all JAR files in the system, for example), and Tomcat's classloaders make them more expensive (builds two string copies of the list of all JAR files in useless debug routines called by the Sun code), so you could wind up adding many hundred milliseconds by using this textbook pattern. IBM's Java implementation may have better performance, but still you should take the opportunity to reduce calls to the two factory methods, because by spec they have expensive behavior. Cache the result of DocumentBuilderFactory.newInstance() statically, and create one directly if you have an implementation in mind (Saxon, Xerces). Finally, if you application has thread control, you can re-use the DocumentBuilder if you can do it safely in a thread, and just call reset on it aftewards. public abstract class DOMUtil { public static org.w3c.dom.Document newDocument() { return getPrivateDocumentBuilder().newDocument(); } public static org.w3c.dom.Document domParse(InputSource source) throws SAXException, IOException { return newDocumentBuilder().parse(source); } public static org.w3c.dom.Document domParse(InputStream stream) throws SAXException, IOException { return newDocumentBuilder().parse(stream); } public static org.w3c.dom.Document domParse(String uri) throws SAXException, IOException { return newDocumentBuilder().parse(uri); } public static org.w3c.dom.Document domParse(File file) throws SAXException, IOException { return newDocumentBuilder().parse(file); } static DocumentBuilderFactory documentBuilderFactory; static DocumentBuilder privateDocumentBuilder; /** * DocumentBuilderFactory is expensive as it searches classpath, and calls * javax.xml.parsers.FactoryFinder.findJarServiceProvider calls loader.toString * on the Tomcat loader, and that's really expensive as it contains a list of jars. * See somewhat related bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5047031 * and discussion at http://www.nabble.com/JAXP-performance-problems-t2279828.html */ public static DocumentBuilderFactory getDocumentBuilderFactory() { synchronized(DOMUtil.class) { if (documentBuilderFactory == null) { // Don't do DocumentBuilderFactory.newInstance() // because JDK 1.5.0_008 has debug statements in javax.xml.parsers.DocumentBuilderFactory // that needlessly call .toString() on the classloader, and the toString method on the Tomcat classloader // allocates two StringBuffers and creates a 20K character String. So we just skip the // DocumentBuilderFactory.newInstance() and use its default // implementation for JDK 1.5 directly. Change to use Saxon or your favorite implementation. // documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl(); documentBuilderFactory.setValidating(false); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setExpandEntityReferences(true); } return documentBuilderFactory; } } /** * Don't call this if you can avoid it; use newDocument or domParse instead. */ public static DocumentBuilder newDocumentBuilder() { try { return getDocumentBuilderFactory().newDocumentBuilder(); } catch (ParserConfigurationException pex) { // This can't happen unless the system is misconfigured. throw new RuntimeException(pex.getMessage()); } } // never pass this static object this class as it's not Thread-safe private static DocumentBuilder getPrivateDocumentBuilder() { if (privateDocumentBuilder == null) { try { privateDocumentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); } catch (ParserConfigurationException pex) { // This can't happen unless the system is misconfigured. throw new RuntimeException(pex.getMessage()); } } else { privateDocumentBuilder.reset(); } return privateDocumentBuilder; } } -----Original Message----- From: www-forms-request@... [mailto:www-forms-request@...] On Behalf Of Aaron Reed Sent: Friday, February 16, 2007 12:04 PM To: www-forms@... Subject: Re: sending xml response from servlet Hi Iñaki, I am no servlet or java guru by any stretch of the imagination, but this is what I did to build a simple servlet that got a xml document from xforms, tweaked a few text nodes in it (via the method changePassword()), and then sent back a xml response to replace the instance that was sent: public void doWork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String contentType = req.getContentType(); if(contentType.equalsIgnoreCase("text/xml") || contentType.equalsIgnoreCase("application/xml")) { InputStream inputStream = req.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(inputStream); if(doc != null) { boolean result = changePassword(doc); if (result == true) { resp.setContentType(contentType); // following code serializes dom to xml file OutputFormat of = new OutputFormat(doc); of.setIndenting(false); ServletOutputStream outputStream = resp.getOutputStream(); XMLSerializer serializer = new XMLSerializer(); serializer.setOutputFormat(of); serializer.setOutputByteStream(outputStream); DOMSerializer domSerializer = serializer.asDOMSerializer(); domSerializer.serialize(doc); outputStream.flush(); outputStream.close(); } } } catch (Exception e) { e.printStackTrace(); } } } Again, I don't know if this is the way a 'real' app designer would do it, but it worked for me in my little test scenario. Good luck, --Aaron Iñaki Salinas Bueno wrote: > Hello, > > Can someone recommend me a set of libraries that allow a servlet > receive/send XML documents from/to xforms? I have found several > libraries, but I don't know which is more adapted for what I want to do. > > I'm using xforms in client side and a servlet for xindice (DB manager) > calls in server side. > > The servlet gets the xml document from xforms and add it in the DB > correctly (I used a Xindice web application example and the tip 'Xforms > tip: Accepting XForms data in Java > <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' > for its construction), but I don't know how can I get a XML document > from DB and put it in the response object of the servlet. > > The example of the tip works with strings, so following it for the > response I would have to take the XML document of the DB, transform it > into a string, and then send it. Cannot be the XML document sent as > application/xml without transforming it into a string? > > Maybe questions are more java related than xforms, but they are > working-with-xml related so I think that I can found help in this forum. > > Thanks > Iñaki |
|
|
Re: sending xml response from servletIñaki, We have a very simple example in JSP in our CVS (scroll past the tags...): http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/ops/orbeon/src/examples-jsp/flickr-search/service-search.jsp?rev=1.1&content-type=text/vnd.viewcvs-markup This example uses dom4j to read the submitted XML. BTW, I recommend you look at the eXist database instead of Xindice. This will also allow you to have your forms directly talk with the database using REST, instead of using a Java layer in the middle: http://exist.sourceforge.net/ We have examples on our web site on how to access eXist from XForms, including in our tutorial: http://www.orbeon.com/ops/doc/intro-tutorial http://www.orbeon.com/ops/xforms-bookcast/ Our Government Forms example also uses REST to directly talk to eXist: http://www.orbeon.com/ops/forms/ I hope this helps, -Erik Iñaki Salinas Bueno wrote: > Hello, > > Can someone recommend me a set of libraries that allow a servlet > receive/send XML documents from/to xforms? I have found several > libraries, but I don't know which is more adapted for what I want to do. > > I'm using xforms in client side and a servlet for xindice (DB manager) > calls in server side. > > The servlet gets the xml document from xforms and add it in the DB > correctly (I used a Xindice web application example and the tip 'Xforms > tip: Accepting XForms data in Java > <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' > for its construction), but I don't know how can I get a XML document > from DB and put it in the response object of the servlet. > > The example of the tip works with strings, so following it for the > response I would have to take the XML document of the DB, transform it > into a string, and then send it. Cannot be the XML document sent as > application/xml without transforming it into a string? > > Maybe questions are more java related than xforms, but they are > working-with-xml related so I think that I can found help in this forum. > > Thanks > Iñaki -- Orbeon Forms - Web Forms for the Enterprise Done the Right Way http://www.orbeon.com/ |
|
|
Re: sending xml response from servletThanks all.
I did so many things and I had to change the code so many times trying different forms to implement my application that I do not know which code I have used. But I am very happy with the help you gave to me. I tried to do it with JDOM library and I embroiled with it. I know why I embroiled now (ignorance of how to use the Java language), I will try it again later maybe. Referring to the last comment of Leigh, I suppose that charset=ISO-8859-1 works too. And thanks for your comments about improving efficiency. It is not relevant for what I have to do but I will consider it if efficiency turns relevant. Regards, Iñaki 2007/2/19, Erik Bruchez <ebruchez@...>:
|
|
|
RE: sending xml response from servletIñaki,
Whatever API you use, when you serialize, you will need to
tell the serializer what encoding to use, and specify that same value as the
charset in the HTTP response header.
ISO-8859-1 will work, but you must tell JDOM to use it; otherwise it will likely default to something else. XML that starts wtih <?xml version="1.0" ?> is UTF-8
by definition and so is the default.
So you really have 3 places to worry about but the XML API
you use will handle two of them (the XML declaration and the actual characters
themselves).
You must make sure that the HTTP response header has the
right value in it.
If this isn't clear, please ask me.
Leigh. From: www-forms-request@... [mailto:www-forms-request@...] On Behalf Of Iñaki Salinas Bueno Sent: Monday, February 19, 2007 12:25 PM To: www-forms Subject: Re: sending xml response from servlet I did so many things and I had to change the code so many times trying different forms to implement my application that I do not know which code I have used. But I am very happy with the help you gave to me. I tried to do it with JDOM library and I embroiled with it. I know why I embroiled now (ignorance of how to use the Java language), I will try it again later maybe. Referring to the last comment of Leigh, I suppose that charset=ISO-8859-1 works too. And thanks for your comments about improving efficiency. It is not relevant for what I have to do but I will consider it if efficiency turns relevant. Regards, Iñaki 2007/2/19, Erik Bruchez <ebruchez@...>:
|
|
|
Re: sending xml response from servlet- putting the corresponding HTTP response header to ISO-8859-1 encoding - telling the serializer of the API that must use the encoding ISO-8859-1 - writing the characters in the document following the ISO-8859-1 (and the starting XML definition = <?xml version="1.0" encoding="ISO-8859-1"?>) Thanks for this information. Returning to my previous problems, outputting data was already solved, but I have been having troubles with the XML document treatment (i.e. setting data in elements, I am incapable to find some method similar to this: setElementValue(String elementName, String elementValue)). So I will finally use the JAXB API. I have been reading about it and I think that it is what I need. I have XML Schemas that define my XML documents, and it seems easy to treat the entire XML document with its binding capabilities. And I do not think that working with other APIs (as Xindice) using JAXB is going to be more difficult that with other APIs. Greetings, Iñaki 2007/2/20, Klotz, Leigh <Leigh.Klotz@...>:
|
|
|
Re: sending xml response from servletLeigh & all, A correction: if you use the media type application/xml, which is the recommended media type for XML, i.e. you produce the header: Content-Type: application/xml then you must not add an encoding (charset) to that, because charset is defined for text media types. XML is actually not a text format in the usual sense, because of the XML declaration which can contain the encoding (and the default to UTF-8 if you don't have an XML declaration or no encoding in the declaration). In short, this is what I recommend: o Use application/xml o If you want to specify an output encoding other than UTF-8, do it by setting the appropriate parameter on your XML serialization API. o Always use the API to serialize XML to a binary stream rather than a text stream. -Erik Klotz, Leigh wrote: > Iñaki, > Whatever API you use, when you serialize, you will need to tell the > serializer what encoding to use, and specify that same value as the > charset in the HTTP response header. > ISO-8859-1 will work, but you must tell JDOM to use it; otherwise it > will likely default to something else. > XML that starts wtih <?xml version="1.0" ?> is UTF-8 by definition and > so is the default. > So you really have 3 places to worry about but the XML API you use will > handle two of them (the XML declaration and the actual characters > themselves). > You must make sure that the HTTP response header has the right value in it. > If this isn't clear, please ask me. > Leigh. > > ------------------------------------------------------------------------ > *From:* www-forms-request@... [mailto:www-forms-request@...] *On > Behalf Of *Iñaki Salinas Bueno > *Sent:* Monday, February 19, 2007 12:25 PM > *To:* www-forms > *Subject:* Re: sending xml response from servlet > > Thanks all. > > I did so many things and I had to change the code so many times trying > different forms to implement my application that I do not know which > code I have used. But I am very happy with the help you gave to me. > > I tried to do it with JDOM library and I embroiled with it. I know why I > embroiled now (ignorance of how to use the Java language), I will try it > again later maybe. > > Referring to the last comment of Leigh, I suppose that > charset=ISO-8859-1 works too. And thanks for your comments about > improving efficiency. It is not relevant for what I have to do but I > will consider it if efficiency turns relevant. > > Regards, > Iñaki > > > > 2007/2/19, Erik Bruchez <ebruchez@... <mailto:ebruchez@...>>: > > > Iñaki, > > We have a very simple example in JSP in our CVS (scroll past the > tags...): > > http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/ops/orbeon/src/examples-jsp/flickr-search/service-search.jsp?rev=1.1&content-type=text/vnd.viewcvs-markup > <http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/ops/orbeon/src/examples-jsp/flickr-search/service-search.jsp?rev=1.1&content-type=text/vnd.viewcvs-markup> > > This example uses dom4j to read the submitted XML. > > BTW, I recommend you look at the eXist database instead of Xindice. This > will also allow you to have your forms directly talk with the database > using REST, instead of using a Java layer in the middle: > > http://exist.sourceforge.net/ > > We have examples on our web site on how to access eXist from XForms, > including in our tutorial: > > http://www.orbeon.com/ops/doc/intro-tutorial > http://www.orbeon.com/ops/xforms-bookcast/ > > Our Government Forms example also uses REST to directly talk to eXist: > > http://www.orbeon.com/ops/forms/ > > I hope this helps, > > -Erik > > Iñaki Salinas Bueno wrote: > > Hello, > > > > Can someone recommend me a set of libraries that allow a servlet > > receive/send XML documents from/to xforms? I have found several > > libraries, but I don't know which is more adapted for what I want > to do. > > > > I'm using xforms in client side and a servlet for xindice (DB > manager) > > calls in server side. > > > > The servlet gets the xml document from xforms and add it in the DB > > correctly (I used a Xindice web application example and the tip > 'Xforms > > tip: Accepting XForms data in Java > > > <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' > > for its construction), but I don't know how can I get a XML document > > from DB and put it in the response object of the servlet. > > > > The example of the tip works with strings, so following it for the > > response I would have to take the XML document of the DB, > transform it > > into a string, and then send it. Cannot be the XML document sent as > > application/xml without transforming it into a string? > > > > Maybe questions are more java related than xforms, but they are > > working-with-xml related so I think that I can found help in this > forum. > > > > Thanks > > Iñaki > > > -- > Orbeon Forms - Web Forms for the Enterprise Done the Right Way > http://www.orbeon.com/ > > -- Orbeon Forms - Web Forms for the Enterprise Done the Right Way http://www.orbeon.com/ |
|
|
RE: sending xml response from servletI agree with Erik except that on #1 I believe you will find web clients where sending charset=UTF-8 necessary though against recommendations. I intended to say #2. -----Original Message----- From: www-forms-request@... [mailto:www-forms-request@...] On Behalf Of Erik Bruchez Sent: Tuesday, February 20, 2007 1:00 PM To: www-forms Subject: Re: sending xml response from servlet Leigh & all, A correction: if you use the media type application/xml, which is the recommended media type for XML, i.e. you produce the header: Content-Type: application/xml then you must not add an encoding (charset) to that, because charset is defined for text media types. XML is actually not a text format in the usual sense, because of the XML declaration which can contain the encoding (and the default to UTF-8 if you don't have an XML declaration or no encoding in the declaration). In short, this is what I recommend: o Use application/xml o If you want to specify an output encoding other than UTF-8, do it by setting the appropriate parameter on your XML serialization API. o Always use the API to serialize XML to a binary stream rather than a text stream. -Erik Klotz, Leigh wrote: > Iñaki, > Whatever API you use, when you serialize, you will need to tell the > serializer what encoding to use, and specify that same value as the > charset in the HTTP response header. > ISO-8859-1 will work, but you must tell JDOM to use it; otherwise it > will likely default to something else. > XML that starts wtih <?xml version="1.0" ?> is UTF-8 by definition and > so is the default. > So you really have 3 places to worry about but the XML API you use will > handle two of them (the XML declaration and the actual characters > themselves). > You must make sure that the HTTP response header has the right value in it. > If this isn't clear, please ask me. > Leigh. > > ------------------------------------------------------------------------ > *From:* www-forms-request@... [mailto:www-forms-request@...] *On > Behalf Of *Iñaki Salinas Bueno > *Sent:* Monday, February 19, 2007 12:25 PM > *To:* www-forms > *Subject:* Re: sending xml response from servlet > > Thanks all. > > I did so many things and I had to change the code so many times trying > different forms to implement my application that I do not know which > code I have used. But I am very happy with the help you gave to me. > > I tried to do it with JDOM library and I embroiled with it. I know why I > embroiled now (ignorance of how to use the Java language), I will try it > again later maybe. > > Referring to the last comment of Leigh, I suppose that > charset=ISO-8859-1 works too. And thanks for your comments about > improving efficiency. It is not relevant for what I have to do but I > will consider it if efficiency turns relevant. > > Regards, > Iñaki > > > > 2007/2/19, Erik Bruchez <ebruchez@... <mailto:ebruchez@...>>: > > > Iñaki, > > We have a very simple example in JSP in our CVS (scroll past the > tags...): > > http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/ops/orbeon/src/examples-jsp/flickr-search/service-search.jsp?rev=1.1&content-type=text/vnd.viewcvs-markup > <http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/ops/orbeon/src/examples-jsp/flickr-search/service-search.jsp?rev=1.1&content-type=text/vnd.viewcvs-markup> > > This example uses dom4j to read the submitted XML. > > BTW, I recommend you look at the eXist database instead of Xindice. This > will also allow you to have your forms directly talk with the database > using REST, instead of using a Java layer in the middle: > > http://exist.sourceforge.net/ > > We have examples on our web site on how to access eXist from XForms, > including in our tutorial: > > http://www.orbeon.com/ops/doc/intro-tutorial > http://www.orbeon.com/ops/xforms-bookcast/ > > Our Government Forms example also uses REST to directly talk to eXist: > > http://www.orbeon.com/ops/forms/ > > I hope this helps, > > -Erik > > Iñaki Salinas Bueno wrote: > > Hello, > > > > Can someone recommend me a set of libraries that allow a servlet > > receive/send XML documents from/to xforms? I have found several > > libraries, but I don't know which is more adapted for what I want > to do. > > > > I'm using xforms in client side and a servlet for xindice (DB > manager) > > calls in server side. > > > > The servlet gets the xml document from xforms and add it in the DB > > correctly (I used a Xindice web application example and the tip > 'Xforms > > tip: Accepting XForms data in Java > > > <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' > > for its construction), but I don't know how can I get a XML document > > from DB and put it in the response object of the servlet. > > > > The example of the tip works with strings, so following it for the > > response I would have to take the XML document of the DB, > transform it > > into a string, and then send it. Cannot be the XML document sent as > > application/xml without transforming it into a string? > > > > Maybe questions are more java related than xforms, but they are > > working-with-xml related so I think that I can found help in this > forum. > > > > Thanks > > Iñaki > > > -- > Orbeon Forms - Web Forms for the Enterprise Done the Right Way > http://www.orbeon.com/ > > -- Orbeon Forms - Web Forms for the Enterprise Done the Right Way http://www.orbeon.com/ |
|
|
RE: sending xml response from servletNo, #3 happens automatically if you do #2. Erik
disagrees me with on #1 so you need to make your own decision there based on
what your software needs. For #2, Erik and I agree.
Leigh. From: www-forms-request@... [mailto:www-forms-request@...] On Behalf Of Iñaki Salinas Bueno Sent: Tuesday, February 20, 2007 11:45 AM To: www-forms Subject: Re: sending xml response from servlet - putting the corresponding HTTP response header to ISO-8859-1 encoding - telling the serializer of the API that must use the encoding ISO-8859-1 - writing the characters in the document following the ISO-8859-1 (and the starting XML definition = <?xml version="1.0" encoding="ISO-8859-1"?>) Thanks for this information. Returning to my previous problems, outputting data was already solved, but I have been having troubles with the XML document treatment (i.e. setting data in elements, I am incapable to find some method similar to this: setElementValue(String elementName, String elementValue)). So I will finally use the JAXB API. I have been reading about it and I think that it is what I need. I have XML Schemas that define my XML documents, and it seems easy to treat the entire XML document with its binding capabilities. And I do not think that working with other APIs (as Xindice) using JAXB is going to be more difficult that with other APIs. Greetings, Iñaki 2007/2/20, Klotz, Leigh <Leigh.Klotz@...>:
|
|
|
Re: sending xml response from servletHi Aaron,
Could you please post your whole servlet code with the sevlet doPost?It would be really helpful. Thank You. Celarin
|
|
|
Re: sending xml response from servletHi Celarin, It took me a while to find it. I don't have it currently running on a server to test it, but it should work. Let me know if it doesn't. Don't forget to fix the line wrapping before you try to use it. import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.xml.serialize.DOMSerializer; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * @version 1.0 * @author */ public class ChangePassword extends HttpServlet { /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet hit"); doWork(req, resp); } /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost hit"); doWork(req, resp); } public void doWork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String contentType = req.getContentType(); if(contentType.equalsIgnoreCase("text/xml") || contentType.equalsIgnoreCase("application/xml")) { InputStream inputStream = req.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(inputStream); if(doc != null) { boolean result = changePassword(doc); if (result == true) { resp.setContentType(contentType); // following code serializes dom to xml file OutputFormat of = new OutputFormat(doc); of.setIndenting(false); ServletOutputStream outputStream = resp.getOutputStream(); XMLSerializer serializer = new XMLSerializer(); serializer.setOutputFormat(of); serializer.setOutputByteStream(outputStream); DOMSerializer domSerializer = serializer.asDOMSerializer(); domSerializer.serialize(doc); outputStream.flush(); outputStream.close(); } } } catch (Exception e) { e.printStackTrace(); } } } public boolean changePassword(Document doc) { Element rootElement = doc.getDocumentElement(); if (rootElement != null) { NodeList list = rootElement.getElementsByTagName("password"); if (list != null ) { int len = list.getLength(); for (int i=0; i < len; i++) { Node password = list.item(i); Node passwordText = password.getFirstChild(); if (passwordText.getNodeType()!=Node.TEXT_NODE) { return false; } String value = passwordText.getNodeValue(); StringBuffer strbuff = new StringBuffer(value); int valueLen = strbuff.length(); for (int j=0; j < valueLen; j++) { char oldchar = strbuff.charAt(j); strbuff.setCharAt(j, (char)(oldchar+1)); } passwordText.setNodeValue(strbuff.toString()); } } } return true; } } --Aaron celarin wrote: > Hi Aaron, > Could you please post your whole servlet code with the sevlet doPost?It > would be really helpful. > Thank You. > Celarin > > Aaron Reed wrote: >> >> Hi Iñaki, >> >> I am no servlet or java guru by any stretch of the imagination, but this >> is what I did to build a simple servlet that got a xml document from >> xforms, tweaked a few text nodes in it (via the method >> changePassword()), and then sent back a xml response to replace the >> instance that was sent: >> >> public void doWork(HttpServletRequest req, HttpServletResponse resp) >> throws ServletException, IOException { >> >> String contentType = req.getContentType(); >> if(contentType.equalsIgnoreCase("text/xml") || >> contentType.equalsIgnoreCase("application/xml")) { >> InputStream inputStream = req.getInputStream(); >> DocumentBuilderFactory factory = >> DocumentBuilderFactory.newInstance(); >> try { >> DocumentBuilder docBuilder = factory.newDocumentBuilder(); >> Document doc = docBuilder.parse(inputStream); >> if(doc != null) { >> boolean result = changePassword(doc); >> if (result == true) { >> resp.setContentType(contentType); >> // following code serializes dom to xml file >> >> OutputFormat of = new OutputFormat(doc); >> of.setIndenting(false); >> ServletOutputStream outputStream = resp.getOutputStream(); >> XMLSerializer serializer = new XMLSerializer(); >> serializer.setOutputFormat(of); >> serializer.setOutputByteStream(outputStream); >> DOMSerializer domSerializer = serializer.asDOMSerializer(); >> domSerializer.serialize(doc); >> outputStream.flush(); >> outputStream.close(); >> } >> } >> } >> catch (Exception e) { >> e.printStackTrace(); >> } >> } >> >> } >> >> Again, I don't know if this is the way a 'real' app designer would do >> it, but it worked for me in my little test scenario. >> >> Good luck, >> --Aaron >> >> >> Iñaki Salinas Bueno wrote: >>> Hello, >>> >>> Can someone recommend me a set of libraries that allow a servlet >>> receive/send XML documents from/to xforms? I have found several >>> libraries, but I don't know which is more adapted for what I want to do. >>> >>> I'm using xforms in client side and a servlet for xindice (DB manager) >>> calls in server side. >>> >>> The servlet gets the xml document from xforms and add it in the DB >>> correctly (I used a Xindice web application example and the tip 'Xforms >>> tip: Accepting XForms data in Java >>> <http://www-128.ibm.com/developerworks/java/library/x-xformstipjava/index.html>' >>> for its construction), but I don't know how can I get a XML document >>> from DB and put it in the response object of the servlet. >>> >>> The example of the tip works with strings, so following it for the >>> response I would have to take the XML document of the DB, transform it >>> into a string, and then send it. Cannot be the XML document sent as >>> application/xml without transforming it into a string? >>> >>> Maybe questions are more java related than xforms, but they are >>> working-with-xml related so I think that I can found help in this forum. >>> >>> Thanks >>> Iñaki >> >> >> >> > |
| Free embeddable forum powered by Nabble | Forum Help |