PRG design pattern of web development

Shreyansh Gupta
Nerd For Tech
Published in
2 min readJul 31, 2023

I’ve been learning development in Java. As a part of the learning process, I built a Todo app using the Java Servlet API. Problems I faced during the development, introduced me to the PRG design pattern.

A design pattern is a pre-defined way of solving a problem.

PRG stands for Post/Redirect/Get design pattern.

Let’s take a look at the demo for the Todo app —

The Problem

Notice how the Todo create form is on the same page as the list of Todos. When we submit the form, a POST request is sent. If we return the updated Todo list in the response for the POST request, then refreshing the page would cause the form to resubmit.

Let’s understand this with the help of a diagram —

Causes POST request to be resent

The Solution

The solution is pretty straightforward. In the response to the POST request, instead of sending the list of todos, we send a redirect response. This redirect response, requests the client browser to redirect to the todo list page using a GET request.

Check the diagram below —

Post/Redirect/Get design pattern

Subsequently refreshing the page, does not resubmit the form. Instead, it just queries the todo list page.

This design pattern is called Post/Redirect/Get design pattern.

Further reading

I found the following article a very good explanation on this topic. You can check if out if you’d like to know more.

https://www.theserverside.com/news/1365146/Redirect-After-Post

--

--

Shreyansh Gupta
Nerd For Tech

Software Engineer. I like to write about the new things I learn and find interesting. You can also find me here - https://shreyanshgupta.hashnode.dev/