编写以xml格式返回表数据的servlet的简便方法

时间:2010-10-19 06:03:01

标签: java xml jsp servlets

编写以xml格式返回表数据的servlet的简便方法 - ?

数据来自数据库表。

2 个答案:

答案 0 :(得分:3)

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();
    }
}

答案 1 :(得分:2)

您可能会发现jersey对XML有用,也支持JSON。这是一个很好的tutorial(见第4节)。