0

I want to create an XML file through servlet Here is my code

        out.println("<html>") out.println("<head>");
        out.println("<title>Servlet contactReq</title>");            
        out.println("</head>");
        out.println("<body>");
        //PrintWriter out = response.getWriter();
        //System.out.println("Hello you are in get method");
        String fName = request.getParameter("firstName");
        String lName = request.getParameter("lastName");
        String email = request.getParameter("email");
        int phNo = Integer.parseInt(request.getParameter("phoneNo"));
        add(fName,lName,email,phNo,response);
        out.println("<h1>Successssssssss :"+fName+"</h1>");
        out.println("</body>");
        out.println("</html>");`

In add() I used this

File f = new File("src"+File.separator+"xmlparse");
f.createNewFile();//create the file
out.println("<h2>Root:"+f.getAbsolutePath()+"</h2>");

but I didn't get this root :..... , my file was not created and if I want to write logic in servlet. Is this the proper way?

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
Durgaprasad
  • 157
  • 2
  • 6
  • 19

2 Answers2

0

It looks like you are trying to create an HTML page to be displayed to the browser. I recommend you to forward the request to .jsp file. It will make your life much easier.

request.setAttribute("attributeName", someValue); //someValue can be String, int, or any other serializable object.
RequestDispatcher requestDispatcher = request.getRequestDispatcher("somepage.jsp");  
requestDispatcher.forward(request, response);
Multithreader
  • 878
  • 6
  • 14
0

That's not valid XML, that's poorly formatted HTML.
Java has excellent built in tooling for creating XML and parsing it to a Stream, use those to create the XML and send it to the client. No need to much around with String concatenation, files, and stuff like that.
Something like the following works much more reliable.

        DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = df.newDocumentBuilder();
    Document doc = documentBuilder.newDocument();
    Element root = doc.createElement("RootElement");
    doc.appendChild(root);
    Element child = doc.createElement("ChildElement");
    child.setNodeValue("Hello World");
    root.appendChild(child);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();

    StreamResult resultStream = new StreamResult(response.getOutputStream());
    transformer.transform(new DOMSource(doc), resultStream);

assuming the ServletResponse is called "response" in your service method.
That's of course not complete code, you'll need to handle the plumbing yourself.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • Thanks for for respond,I have already done this part in normal way, but i am going to confused about this one, can you please help me how can i create/write xml file using servlet – Durgaprasad Jul 03 '13 at 10:55
  • @Durgaprasad I just did, this will create xml and send it to the client... I'm not going to write your entire application for you. – jwenting Jul 03 '13 at 10:59