guidance

Since servlets are run in a multi-threaded environment, what should programmers avoid?

a.) Using static variables
b.) Using the marker interface SingleThreadModel
c.) Relying on multi-threaded execution of servlets
d.) Programming with explicit threads in servlets
e.) Putting values in a session object

can some one let me know the correct answer

Using a marker interface is not a problem even though it is a big ambiguous here, you want to use SingleThreadModel whereas you know it is not the case.

Homework? :stuck_out_tongue:

My guess would be a)

a) b) c) d) all make sense as an answer, though statics arn’t any worse than member variables in this case because there’s no guarantee (or expectation) that each session will get a different instance of the servlet class.

Does sound like homework though, so you might want to consider whether we’re giving you the right answers or wrong answers just to confuse.

Kev

even e) is not safe, as the browser makes multiple connections to your server (page + ajax calls OR page + other page). You can run into threading problems when reading/writing into your session.

So… what should be avoided? a), b), d) and e) … not c) because you can’t really avoid “Relying on multi-threaded execution of servlets”, as that’s just what you have to deal with in the first place.

So IMHO the question is wrong (did you type it correctly from your assignment?).

I believe the answer is all of them? obviously with the expectation that you will state your reasoning for each.

How can you avoid c) ?

c) is what you should do.

The answer is f) This is also the grade you may receive in this class. ;D

I would disagree, you shouldn’t rely upon multi-threaded execution, to do so implies your code would cease to function correctly if the servlets were being executed synchronously.

All of them, while I consider e) to be difficult to avoid in a real web application.