W3C home > Mailing lists > Public > www-forms@w3.org > February 2007

Re: sending xml response from servlet

From: Aaron Reed <aaronr@us.ibm.com>
Date: Fri, 16 Feb 2007 14:04:28 -0600
To: www-forms@w3.org
Message-ID: <er52s1$2uv$1@sea.gmane.org>

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
Received on Friday, 16 February 2007 20:16:43 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Saturday, 10 March 2012 06:22:09 GMT