I am porting my Java servlet front controller from a large if-else if block to the command pattern and have created a command interface with an execute method. Currently, I am instantiating an instance of each command in the init() method of my servlet and storing them in a HashMap. I am wondering how I can run the necessary command.execute() within the context of a given request?
Do I add a setContext(HttpServletRequest request, HttpServletResponse response); method to the interface and call command.setContext(request, response) from my doGet()/doPost() methods before I execute or should I not be instantiating the commands in init() to begin with? instead, having a constructor that takes request and response as args?
Obviously, the aim of the command is to set various attributes for a given user/session and determine the correct JSP to forward to, which it can't really do without the context.
requestandresponseit does something with it.HashMapinstead and get a new instance for each request. How is the command pattern applied normally in servlets.execute(InputContext, OutputContext)style for this purpose. Don't do the factory stuff. You should only have to use theHttpRequestandHttpResponseinstances in your Command instances. I don't see any other object you'd need that is specific to the request itself, and can not be part of initialization of the Command instances themselves.static.)