What is the best practice to get servlet context in service layer ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a number of options, all of them wrong:
Put it in a
ThreadLocal(i.e. per-request) and get it viaThreadLocalServletContextHolder.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
Mapof attribute and pass it instead.