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) {
	          // following code serializes dom to xml file

               OutputFormat of =  new OutputFormat(doc);
               ServletOutputStream outputStream = resp.getOutputStream();
               XMLSerializer serializer = new XMLSerializer();
               DOMSerializer domSerializer = serializer.asDOMSerializer();
	    catch (Exception e) {


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,

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 
> <>' 
> 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

Received on Friday, 16 February 2007 20:16:43 UTC