My web application is created with Spark Framework (Connecting on same page as server) the url of the first page is http://localhost:4567/start From here the user clicks on a button to decide one of four tasks. The form action button is /start
The server checks everything is okay and then returns the new page for this task (e.g fixsongs) (i.e returns the page contents as a string ) from the page.
post(RoutePath.START, (request, response) -> new ServerStart().processRoute(request, response));//User has picked task on start page
ServerFixSongs ssfs = new ServerFixSongs();
path(RoutePath.STARTFIXSONGS, () ->
{
//Display Page
post(RoutePath.FIX, (request, response) -> ssfs.startTask(request, response)); //User submits form on Options page
});
The problem is url stays the same, i.e is one behind where the user is
Now I have worked out how to solve this, instead of the server returning page it now does a redirect to /fixsongs.go (this is mapped in routes) which calls method that then returns the page contents as a string and modifys the url.
post(RoutePath.START, (request, response) -> new ServerStart().processRoute(request, response));//User has picked task on start page
ServerFixSongs ssfs = new ServerFixSongs();
path(RoutePath.STARTFIXSONGS, () ->
{
get(RoutePath.GO, (request, response) -> new FixSongsPage(request.session().attribute(FOLDER)).createPage(null, null)); //Display Page
post(RoutePath.FIX, (request, response) -> ssfs.startTask(request, response)); //User submits form on Options page
});
But I have two questions
- Is this more cumbersome approach the correct way to do this
- Do these additional redirect steps impact performance
Note I am not using templating but creating webpages using j2html
I cannot do a redirect directly to a html file in the first call since the html does not actually exist, the pages are created dynamically.
I have also realised that although when I submit start task from START page i submit a POST request because Im redirecting to STARTFIXSONGS this means at the next stage user can use BACK button to go back to STARTFIXSONGS. I would prefer they could not do this, so does this mean i shoud not be using redirects in this case.