Java Servlet JSON Response in JSP using Gson

In Modern web application We get data from server in JSON format and display data in jsp or html page using Front end library or frameworks like Angular, React etc.

This is similar for mobile app development.

Here we will see how we can get JSON Response from servlet and show in jsp.

What is JSON ?

JavaScript Object Notation (JSON) is a lightweight format for interchange data.

  • JSON is a Good Human readable format
  • JSON is derived from JavaScript
  • JSON is text based format for representing structured data
  • JSON is key value pair data
  • JSON is language independent

These are features of JSON.

JSON can be used to interchange data between different platform.

Due to lightweight it is very fast to exchange information between different application i.e. web based applications.

So knowing how to use JSON with servlet will good

Creating Java web project in NetBeans

We already know how to create simple projects with netbeans see project creating with maven and creating simple web project in netbeans.

We created a Java Web project ServletJson to get Response from Servlet JSON

Project Explorer

Netbeans Project Explorer

In above project

  1. Created Pojo for Studnet
  2. Created Pojo for Address
  3. Created Pojo for Subject
  4. Created JsonResponse Servlet
  5. created index.jsp

Creating Classes and Pages In Netbeans

index.jsp

This file contains a link on click that will goto url

localhost:8084/ServletJson/jsonResponse

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>

JsonResponse.java

Here we want to send json data response from servlet.
Here a link is available in jsp page on link click it is calling /jsonResponse url where JsonResponse’s doGet() method is get called.

How to set HttpServletResponse set body json

To generate json response we are setting response.setContentType("application/json"); that is used to set response as application/json.

Here response is reference variable of HttpServletResponse.

In servlet student object is created with name gender mobile number and three subjects marks. After creating object we are converting objects to json using gson library.

response.setContentType("application/json"); is used to send response as json.

response.setCharacterEncoding("utf-8"); is character encoding method.

We have created student object which contains students id,name,gender,address, mobileNo and set of subjects and address.

We send this data to jsp using Gson Object.

The method gson.toJson(student); is converting object to json string

then we are passing json data to PrintWriter object to write in browser

Student.java

This class contains student data like id, name , gender ,address, mobile number and multiple subjects mark.

Subject.java

contains id and name of subject.

Address

contain address fields id , street, city,state and country fields

Result

This is index.jsp page after running the project on click on link it will show json data in browser

Jsp Page to generate JSON Response

java servlet return json

JSON Response In JSP page from Servlet

java servlet return json

Above java servlet return JSON response.

Downlaod above program from here