-1

Servlets share their instances among many clients. So using instance field is bad to store personal information. Is it better to use session attributes as instance fields in servlets?

So session.setAttribute("totalPrice", 50) instead of private int totalPrice = 50

user2997204
  • 1,344
  • 2
  • 12
  • 24

1 Answers1

0

I guess you are trying to store some information between requests for a particular client?

Well, Sessions are the suitable place to store this data.

If you store data as a field in a Servlet, the next request made by the client might be served by a different instance of the Servlet and therefore not have the field variable. Worse, the field might have been set by another client when the Servlet served a different client.

Ramsay Domloge
  • 695
  • 3
  • 11
  • And what is the best approach if this information is not used between multiple requests but used in one request? – user2997204 Jul 10 '16 at 20:34