How to redirect to a different VF page from the constructor with parameters in Salesforce?

Sample Code:

Apex Class:

public with sharing class SampleClass{

    public SampleClass(){
    
    }
    public PageReference doRedirect(){
        String userId = UserInfo.getUserId();
        PageReference pageRef = new PageReference('/apex/TestPage2');
        pageRef.getParameters().put('UserId',userId);
        pageRef.setRedirect(true);
        return pageRef;
    }
}

VF Page:

<apex:page controller="SampleClass" action="{!doRedirect}">
	<h1>Congratulations</h1>
	This is your new Page
</apex:page>