views:

54

answers:

2

Easy way to write servlet that returns table data in xml format - ?

Data comes from a database table.

+2  A: 

You may found jersey useful it works for XML, also supports JSON . Here is a good tutorial (Look section 4) .

ahvargas
+2  A: 
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TableServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("application/xml");
        PrintWriter writer = response.getWriter();
        writer.println("<?xml version=\"1.0\"?>");
        writer.println("<table>");
        writer.println("<style>");
        writer.println("<finish>polished</finish>");
        writer.println("<material>oak</material>");
        writer.println("</style>");
        writer.println("<legs>4</legs>");
        writer.println("</table>");
        writer.flush();
    }
}
Qwerky