Blog

Migrating a legacy JSP system to React, step by step

3 min read

Migrating a legacy JSP system to React, step by step

At work I deal with a large JSP system that has been running for years. People use it every day and business processes depend on it, so it can't be rewritten overnight. We're moving it to React piece by piece, and in this post I'll cover the approach we picked and where most of the time went.

Strangler fig

Instead of replacing the old system in one go, we grow the new one around it. A new module, such as the customer verification (KYC) section, is written in React while the old JSP pages keep working. When a user moves from JSP into the new section, the React app opens, and over time there are fewer and fewer JSP pages.

What I like most about it is that each stage ships on its own, and a bug only touches one module.

Handing off the session

The user is already logged in to the JSP system. Asking them to log in again when they reach the React app is annoying, and passing session data openly in the URL is dangerous.

So we use a one-time handoff token. The JSP side generates a random UUID and stores it with the session data in Redis for a short time (TTL). The user is redirected to the React app with that UUID, the app sends it to the backend, and the backend reads it from Redis and deletes it immediately. In the response, the React app gets its own tokens. The token works once and expires within seconds, so even if the URL stays in the browser history, nobody can reuse it.

The bridge had a surprise in it too. In local development, the Vite proxy routed addresses with and without a trailing slash differently, and because of that the login button stopped working on one page. It wasn't obvious at first that the proxy was to blame.

Parallel 401s

The React app fires several requests at once: the table, filters, reference data. When the access token expires, they all return 401 together. If each of them tries to refresh the token on its own, several refresh requests go out and cancel each other.

We solved it with a queue in an Axios interceptor. The first 401 starts a refresh and sets a flag, and any 401s that arrive meanwhile wait in the queue. When the refresh finishes, every queued request is retried with the new token. If the refresh fails, the queue is cleared and the user goes back to the login page.

Tables

Almost everything in this system is a table, so I wrote a generic CustomTable<T> component on top of the Ant Design table, with TanStack Query inside. Filter, page and sort state lives in the URL, so a link sent to a colleague opens exactly the same view. API calls are created through a createTableApi() factory. If filtering ever moves from GET to POST, only one file changes.

Stack

Vite, React and TypeScript. Ant Design for tables and forms, TanStack Query for server state, Zustand for the little global state there is. Forms use React Hook Form and Zod, translations use i18next, in four languages: Uzbek in Latin and Cyrillic, Russian and English.

Takeaway

Most of the time went into the bridge between the two worlds, not into new pages: session, authentication and navigation. Once we got that part right, the next modules came noticeably faster.