How to Return JSON Response in Servlet

Return JSON Response in Servlet

In this example, we will show simple example about how to return Json response in servlet. The example program has been tested with apache tomcat environment and output are shared in the post.

Project Structure

Return JSON Response in Servlet

Servlet Class (EmployeeService.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.Employee;
import com.dineshkrish.util.JSONConverter;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class EmployeeService extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		// defining the PrintWriter object
		PrintWriter out = response.getWriter();
		
		// setting the response type
		response.setContentType("application/json");
		
		// creating employee object
		Employee employee = new Employee();
		
		// setting the attributes
		employee.setEmpId(101);
		employee.setEmpName("Dinesh Krishnan");
		employee.setEmpAge(25);
		employee.setEmpQualifcation(new String[]{"MS", "MBA"});
		employee.setEmpEmailId("[email protected]");
		employee.setEmpPhone("+91 8989898989");
		
		// converting object to json using Gson api.
		out.println(JSONConverter.convert(employee));
		
		out.close();
	}

	
}

POJO Class (Employee.java)

package com.dineshkrish.pojo;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Employee {

	private int empId;
	private String empName;
	private int empAge;
	private String[] empQualifcation;
	private String empEmailId;
	private String empPhone;

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public int getEmpAge() {
		return empAge;
	}

	public void setEmpAge(int empAge) {
		this.empAge = empAge;
	}

	public String[] getEmpQualifcation() {
		return empQualifcation;
	}

	public void setEmpQualifcation(String[] empQualifcation) {
		this.empQualifcation = empQualifcation;
	}

	public String getEmpEmailId() {
		return empEmailId;
	}

	public void setEmpEmailId(String empEmailId) {
		this.empEmailId = empEmailId;
	}

	public String getEmpPhone() {
		return empPhone;
	}

	public void setEmpPhone(String empPhone) {
		this.empPhone = empPhone;
	}

}

Object to JSON Converter (JSONConverter.java)

package com.dineshkrish.util;

import com.google.gson.Gson;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class JSONConverter {

	private static Gson gson = new Gson();
	
	public static String convert(Object object) {
		
		return gson.toJson(object);
	}
}

Application Home Page index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</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>EmployeeService</servlet-name>
		<servlet-class>com.dineshkrish.service.EmployeeService</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>EmployeeService</servlet-name>
		<url-pattern>/getEmployee</url-pattern>
	</servlet-mapping>
	
</web-app>

File Output

Return JSON Response in Servlet

Return JSON Response in Servlet

References

1. Java EE HttpServletRequest Interface
2. Java EE HttpServletResponse Interface
3. Java EE ServletException Class
4. Java IOException Class

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.