Returning XML Response in Servlet

In this sample application, We will show you how to return XML response in Servlet application. This example application has been tested and shared in the same post.

Project Structure

How to Return XML Response in Servlet

Servlet Class (ProductService.java)

package com.dineshkrish.service;
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;
import com.dineshkrish.pojo.Product;
import com.dineshkrish.util.XMLConverter;
/**
* 
* @author Dinesh Krishnan
*
*/
public class ProductService extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// defining the PrintWriter object
PrintWriter out = response.getWriter();
// setting the response type
response.setContentType("application/xml");
// creating product object
Product product = new Product();
// setting the attributes
product.setProductCode(10001);
product.setProductName("Bread");
product.setProductDescription("Fresh Bread");
// converting object to xml using JAX-B api.
out.println(XMLConverter.convert(product));
out.close();
}
}

POJO Class (Product.java)

package com.dineshkrish.pojo;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 
* @author Dinesh Krishnan
*
*/
@XmlRootElement
public class Product {
private int productCode;
private String productName;
private String productDescription;
public int getProductCode() {
return productCode;
}
public void setProductCode(int productCode) {
this.productCode = productCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
}

Object to XML Converter (XMLConverter.java)

package com.dineshkrish.util;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.dineshkrish.pojo.Product;
/**
* 
* @author Dinesh Krishnan
*
*/
public class XMLConverter {
public static String convert(Product product) {
StringWriter writer = new StringWriter();
if (product != null) {
try {
JAXBContext context = JAXBContext.newInstance(Product.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(product, writer);
} catch (JAXBException e) {
e.printStackTrace();
}
}
return writer.toString();
}
}

Application Home Page (index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>XML Web Service</title>
</head>
<body>
<h1>Service is running...</h1>
</body>
</html>

Configuration File (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>WebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ProductService</servlet-name>
<servlet-class>com.dineshkrish.service.ProductService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductService</servlet-name>
<url-pattern>/getProduct</url-pattern>
</servlet-mapping>
</web-app>

Output

How to Return XML Response in Servlet

References

1. Converting Java Object to XML

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *