views:

26

answers:

1

What is the best practice to get servlet context in service layer ?

+3  A: 

There are a number of options, all of them wrong:

  • Put it in a ThreadLocal (i.e. per-request) and get it via ThreadLocalServletContextHolder.get()

  • Pass it as an argument to service methods

They are wrong, because the service layer should not know that it's servlets that are supplying it with data. Tomorrow it might be swing.

So the correct way is to gather all required parameters in the servlet (or controller), and pass them as arguments to the service layer. A worse option, but still not that bad is to get the Map of attribute and pass it instead.

Bozho
thanks, i will pass my required object as argumnet
Zeeshan