Java Reference
In-Depth Information
implements javax.servlet.Servlet {
public SimpleRestServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DoGet invoked on RESTful service.");
ProductCatalog catalog = new ProductCatalog();
Source xmlSource = asXml(catalog);
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/xml");
StreamResult st = new StreamResult(out);
try {
Transformer t =
TransformerFactory.newInstance().newTransformer();
t.transform(xmlSource, st);
} catch (Exception e) {
throw new ServletException(e);
}
System.out.println("All done.");
}
private static Source asXml(ProductCatalog pc) throws
ServletException {
System.out.println("Marshalling...");
Source source = null;
Document doc = null;
try {
JAXBContext ctx =
JAXBContext.newInstance(ProductCatalog.class);
Marshaller m = ctx.createMarshaller();
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Search WWH ::




Custom Search