Java Servlet Response JSON in JSP using Gson

In Modern web applications, We get data from the 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 to mobile app development.

Here we will see how we can get JSON Response from the servlet and show it 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 a 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 platforms.

Due to its lightweight, it is very fast to exchange information between different applications 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 projects 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 the servlet.
Here a link is available on the JSP page on the 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 the response as application/json.

Here the response is the reference variable of HttpServletResponse.

In the servlet student object is created with the name gender mobile number and three subject marks. After creating the object we are converting objects to JSON using Gson library.

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

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

We have created a student object which contains student’s 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 an 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 the id and name of the 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

servlet return JSON

java servlet return json

JSON Response In JSP page from Servlet

java servlet return json

Above java servlet return JSON response.

Download above program from here