- From: Aaron Reed <aaronr@us.ibm.com>
- Date: Wed, 08 Oct 2008 01:52:25 -0500
- To: www-forms@w3.org
Hi 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 >> >> >> >> >
Received on Wednesday, 8 October 2008 18:43:18 UTC