3

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

  1. Is this more cumbersome approach the correct way to do this
  2. 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.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

1 Answers1

1

Your question is not really related to Spark or j2html, but rather web-applications in general.

Is this more cumbersome approach the correct way to do this

It depends on what you want. If you want users to be able to navigate using the browser buttons, use the Post-Redirect-Get flow. If you don't want this you should probably be using ajax requests, where you post to the server using JavaScript and the server responds with instructions for updating the DOM (usually in JSON format)

Do these additional redirect steps impact performance

Not enough that you should worry about it. You get one more round-trip, but in 99.9% of cases it doesn't matter. If your target user-base is browsing your page using GPRS on a different continent it would, but that's probably not the case.

tipsy
  • 402
  • 4
  • 15
  • Thanks, it is designed that they dont need to use browser buttons to navigate, but I want it to behave logically if they do use browser buttons, more to the point was that I thought the displayed should represent the current task rather than previous task. Whilst the question was in a way a general question I find answers provided from someone with an understanding of how Spark (or java webaps in general) works make more sense to me than answers from php experts ecetera. So I think Im doing right thing with Post-Redirect-Get in this case. – Paul Taylor Feb 12 '18 at 20:16