How to temporarily store data with good practices?

Asked

Viewed 740 times

1

I have only one page where are all the scripts, when a user clicked on certain data, would redirect it to a specific script and needed to save this data (which he clicked) to put in a case he wanted to edit this data later.

To do this, through JS I took the data, I sent it to the API in Java where I saved it as Session and then return it when necessary through a get method, so that I can put it as value of and it knows which data is editing (No get, JS calls the API in which it returns the/Session data and JS inserts it into value).

I don’t know if this was the best solution, are there others? A better practice? Without having to send the variable via HTML button.

  • 2

    by javascript, you can use sessionStorage or localStorage if you find it more practical: http://zenorocha.com/html5-local-storage/ (this eliminates any sending of browser data to the server)

  • @Leonardobosquett Really, much easier the/

  • And in case it is compatible with old browsers?

  • 2

    very old browsers probably won’t have this feature, take a look here (but see that even IE8 supports the resource): http://caniuse.com/#feat=namevalue-Storage

  • Thank you all! Saved a lot of lines of code. @Lollipop Strange would be man hahaha programmer

1 answer

3


In practice? No problem, if what you want is for an anonymous user or not to fill in a field in which after browsing any other page and returning to this, the field will always be filled in that session, the answer really is this.

Now a long answer: The great constraint that people generally (or at least in my experience) make on saving data on SESSION is in relation to the size of the information you are storing, that is, if this information is not so great there is no problem in doing this. Now what would be a big piece of information? Well, in the case of Session it depends a lot on the capacity of its Server, my recommendation, nothing that exceeds the 100kb of storage, let us take as an example the Cookies that has limit of 20 kb but on the other hand are sent and received to each request, so the SESSION may be a little bigger.

The big question when to choose the type of storage form to adopt is to ask yourself questions such as:

  • The information to store is great?
  • The information is temporary or something that should be saved for a long time?
  • The information is confidential?

With these questions I believe we can come to conclusions, for example: - If not so big (100kb), temporary and confidential, I can save in session - If small (2kb, 5kb), temporary and non-confidential, I can save cookies - If it is large I must save it in a bank or file (hence it would be necessary to identify the user in some way).

Be clear that, I am not imposing rules, keep in mind that rules are very important but nothing is better than common sense, each situation has its best alternative even if it breaks patterns, as great writers have said: rules were made to be broken.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.