Shajirr
Shajirr

Reputation: 163

Saving data of multi-stage jsp forms

In my application I have many different JSP forms with multiple stages, that is after each stage the request is send to the Servlet and processed to generate the next form. The problem is that I need to keep the state of these forms (like in JSF where it is done automatically), so that the user won't have to eneter all the data each time he gets to the error page or navigates back to reenter something he wants to change.

What is the best approach:
1) Store all data in the session, and remove it once the user submitted the final stage of the form.
2) Store it all in the request scope, and read/write each time the user navigates to the different page. However, this does not seem logical since if I save stage 1 and need its data in stage 4, I have to read/write it 3 times instead of just 1 time when using the session scope, and each time the amount of data to be read/written increases.

Upvotes: 2

Views: 1788

Answers (3)

You should handle steps of the form on client side. and send all of the values together. This will save you a lot of work on session values.

Create a div for each step, wrap them with a form tag, show - hide the divs accordingly. also if you use jquery, selection of dom elements would be much easier.

if you have to make a query on the server side, then the obvious choice is session values, assuming that you don't want to use AJAX, nor JSON.

Upvotes: 1

Istvan Devai
Istvan Devai

Reputation: 4022

Both are doable, it depends on your use-case.

  • If you need a calculation after each step and need to send data to the server anyways, session is better.

  • If client is Javascript heavy, you can collect all data and only send it after the final step. This would work only if the steps do not depend on data entered.

If you go for the session approach, be careful to make it work when the user opens your app in multiple tabs. This could be done by detecting the session attribute already in the session, or naming the attribute in a way that does not cause conflict.

Upvotes: 1

Gertjan Assies
Gertjan Assies

Reputation: 1900

session scope should be fine

Just keep in mind that session is usually stored in memory so on very busy webservers it can take up a lot of memory.

Upvotes: 3

Related Questions