How to migrate JSP pages to React? | Enterprise Software House

How to migrate JSP pages to React?

When you become a developer it becomes unavoidable that sooner or later you will be working with legacy code. Even your shiny new code that you are writing today will be a headache for some poor junior programmer 10 or 20 years from now. It is only natural that with time an application that has multiple years of development behind it had to change some technologies. For me an example of such code are pages coded using Java Server Pages. So how can we reconcile this older approach with development in React? In this article I will explain what these technologies are, how are we working with them and some good practices when redesigning JSP pages using React. 

When you become a developer it becomes unavoidable that sooner or later you will be working with legacy code. Even your shiny new code that you are writing today will be a headache for some poor junior programmer 10 or 20 years from now. It is only natural that with time an application that has multiple years of development behind it had to change some technologies. For me an example of such code are pages coded using Java Server Pages. So how can we reconcile this older approach with development in React? In this article I will explain what these technologies are, how are we working with them and some good practices when redesigning JSP pages using React. 

What are JSP pages? 

Java Server Pages are files that allow mixing of HTML and Java code in order to create dynamic websites that combine traditional layout creation using HTML and CSS styling with injected Java code that allows developers to influence the logic of generating the document. It allows easy access to server side methods which considering the amount of data manipulation that some websites needed to do proved rather useful. 

However thanks to time and changing trends approach to creating layout shifted towards React. So how do you migrate existing JSP pages to React? In my experience I settled on a hybrid approach that combines these two technologies. We can create components using React and then inject that component into an existing JSP file. This solution seems like a half measure – why not just replace everything with React? But in reality pages don’t exist in a vacuum – they are a part of an ecosystem where they are referenced in other applications and have their own authentication. This approach can help us with quickly reverting changes to the old way if such need would appear.  

Also it solves another crucial issue: if you are just starting your work on redesigning all those pages you can’t do everything at once. This approach allows us to substitute only a part of given page while leaving other parts intact. Thanks to that our work can be divided into smaller chunks and users don’t have to wait for everything to be finished to start seeing results and giving you feedback. Thanks to our approach we can still work on other parts of the application and leave unaffected JSP elements of the layout alone until they are ready to be remade

How to add React components to JSP pages? 

1. Creating your React component

Your component structure should look like this:

const rootElement: HTMLElement | null = document.getElementById(“your_id");
ReactDOM.render(<YOUR_COMPONENT_NAME/>, rootElement);

 

This will render your component in a specified element with a given id that we will add to the JSP file.

2. Adding webpack entry points

We need to add our component to the webpack definition so that it will generate a bundle for our component. We just need to add our component to the existing entry points definition:

const entryPoints = {
    …,
    component_entry_name: “./src/path_to_component"
};
return {
    mode: args.mode,
    entry: entryPoints,
    output: {
    …
        filename: “[name].js",
    },
    …
}

 

We also need to generate a jsp file containing this newly generated Javascript file. To do that we will use a webpack plugin: 

plugins: [
    …,
    …Object.keys(entryPoints).map(entry =>
        new HtmlWebpackPlugin({
            …
            filename: `${entry}.jsp`,
        }),
    ),
]

 

This will create necessary build files that contain your react code that we will add to JSP HTML in the last step.

 

3. Create a container element in the existing JSP file

 Now you can add an element with your unique id for your component that you defined in the first step (note that this path is the output path form generating webpack files from the previous step) 

<div id="your_id">
    <jsp:include page="../../react/component_entry_name.jsp"/>
</div>

How to migrate code from JSP to React?

Some people might think that When recreating code from JSP we can have problems with the Java code imbedded inside HTML but you might notice that this sounds awfully familiar to JSX from react – HTML code combined with JavaScript. Therefore we should not have any problems with recreating most of the existing logic. However one problem arises when there are any interactions between the rest of JSP elements and the one you just replaced. There is however one trick we can use here – we can make use of event listeners and events to send data between those two technologies. To send data create a custom event and fill it with data you need 

document.dispatchEvent(new CustomEvent(‘edit-user’, {
    detail: {
        variable_name: true,
    },
}));

 

To receive this data you must just add an event listener where you need to receive data – React or a JavaScript file attached to the JSP HTML code: 

document.addEventListener(“edit-user", function (e) {
    if (e.detail.variable_name) {
        console.log(Hello from the other side!)
    }
});

 

Just remember to remove those listeners in the useEffect if you add them in React! 

useEffect(() => {
    …
    return () => {
        document.removeEventListener(“edit-user", …);
    };
}, []);

 

I hope this article explained some of the problems you might encounter when working with JSP and React, however I hope it also showed that working with legacy code can be a great learning experience where you are tested on your knowledge and are forced to come up with creative ideas to your problems – and that’s what’s programming is all about! 

Write us Call us Send email






    1. Personal data is processed pursuant to Article 6 (1) (a) of the Regulation of the European Parliament and of the Council (EU) 2016/679 of April 27, 2016 – the General Data Protection Regulation
    2. The data controller is All for One Poland sp. z o.o. with its registered office in Złotniki, ul. Krzemowa 1 62-002 Suchy Las. Contact data of the Data Protection Supervisor: iod@all-for-one.com.
    3. Consent to data processing is voluntary, but necessary for contact. Consent may be withdrawn at any time without prejudice to the lawfulness of the processing carried out on the basis of consent prior to its withdrawal.
    4. The data will be processed for the purposes stated above and until this consent is withdrawn, and access to the data will be granted only to selected persons who are duly authorised to process it.
    5. Any person providing personal data shall have the right of access to and rectification, erasure, restriction of processing, the right to object to the processing and to the transfer of data, the right to restriction of processing and the right to object to the processing, the right to data transfer.
    6. Every person whose data is processed has the right to lodge a complaint with the supervisory authority, which is the President of the Personal Data Protection Office (ul. Stawki 2, 00-193 Warsaw).
    7. Personal data may be made available to other entities from the group that All for One Poland sp. z o.o. is part of – also located outside the European Economic Area, for marketing purposes. All for One Poland ensures that the data provided to these entities is properly secured, and the person whose data is processed has the right to obtain a copy of the data provided and information on the location of the data provision.

    +48 61 827 70 00

    The office is open
    Monday to Friday
    from 8am to 4pm (CET)

    Question about products and services
    esh@all-for-one.com

    This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.