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

ServletContext getting multiple parameters

index.html

web.xml

ServletContext.java

Results

ServletContext getting parameter

index.html

web.xml

ServletContext.java

ServletContext1.java

Results

ServletConfig to access multiple initial parameter value

index.html

web.xml

ServletConfigMulParam

Result

ServletConfig to access initial parameter value

java servlet are executed when user request for some resource in server. ServletConfig is interface defined in javax.servlet package. ServletConfig is used to access servlet’s initial value stored in web.xml. For each servlet there is one ServletConfig.

Initial value for servlet are stored in web.xml with name value pair as follows

initial parameter are specified inside <servlet> tag as specified above.
A servlet can have one or more <init-param></init-param> tags to store initial values

index.html

web.xml

ServletConfig.java

Result

Getting servlet header

1328_project_explorer
index.jsp

Header.java

pom.xml

Result

1328_first

1328_second

Getting parameter values

Project Explorer :
1317_project_explorer

index.jsp

ParameterValues.java

pom.xml

Result
1317_first

1317_second

getting all request parameters in servlet

before this we have used request.getParameter(String) which is used to get a single request parameter value.
Now if we want to get all parameter names then we have to use request.getParameterNames()
it returns Enumeration of String objects of all parameter names.
After getting all parameter names we can get its value using request.getParameter(String) .
request.getParameterNames() is useful we have large number of parameters are used in request.
Following example is used to explain the above concepts.

Project Explorer:
1309_project_explorer

index.jsp

AllSerlvet.java

pom.xml

Result:
1309_first

1309_seconf

Servlet Maven Configuration Example

Maven is the Project Management Tool, that help developers to built the project easily (make create, add, update, compile, testing and deploy work easy).

  You can download Maven from Official Site

You can find servlet dependency file here

Here we will start a new project with maven

Project Explorer

Maven Project Structure

index.jsp

This Jsp page contains a text box and submit button.

On click on submit form will submit to PersonServlet action.

PersonServlet.java

pom.xml

Result

Servlet Maven example
Servlet Maven Response Page

Request dispatcher forwarding request

For login example we are using user mail id and password. if both are correct then we are forwaring to home.jsp page if any on of mail id or password is in correct then it will forwarded back to index.jsp page with appropriate error message.

1258_project_expo

index.jsp

Login.java

home.jsp

Result

1258_first

If user id and/or password is incorrect then it will be redirected to index.jsp page with appropriate error message.

1258_second

Using correct mailid and password [email protected] and 123456

1258_third