You need to capture that URL somehow. The easiest way is in the Session scope, e.g.
Session.gotoAfterLogin = "/path-where-user-wants-to-go-to";
If that is running in the target script itself, or in onRequestStart() for example, then you can do it like this:
Session.gotoAfterLogin = CGI.SCRIPT_NAME;
Then after a successful login you can use