0

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.

10
  • 3
    You are probably bound for trouble... A single command is at the same time used by multiple requests... That will only work if there is no state to rely on but judging by the fact that you need the request and response it does something with it. Commented Sep 12, 2013 at 11:02
  • @M.Deinum Ah yes, I hadn't thought of that. I guess I could store factories in the HashMap instead and get a new instance for each request. How is the command pattern applied normally in servlets. Commented Sep 12, 2013 at 11:10
  • 1
    As I stated in one of my previous answers, you'd need an execute(InputContext, OutputContext) style for this purpose. Don't do the factory stuff. You should only have to use the HttpRequest and HttpResponse instances 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. Commented Sep 12, 2013 at 11:11
  • 2
    Does neither of the roughly five million Java web frameworks already do this? (And get rid of the ridiculous thing that is JSP, the template engine which has failed to grow error messages with accurate line numbers in the past two decades, as a bonus.) Commented Sep 12, 2013 at 11:20
  • 1
    @ppeterka66 Just because you do need to isolate state besides the request and response doesn't mean it's a good idea to share all other state. Shared state is bad unless proven otherwise, the logic should go the other way around. (And you can always add it with static.) Commented Sep 12, 2013 at 11:26

1 Answer 1

2

You should use:

command.execute(HttpServletRequest request, HttpServletResponse response);

All state can (and should) be recorded in the request. This is easy to do by storing attributes.

Sometimes you may need to use:

command.execute(this, HttpServletRequest request, HttpServletResponse response);

but probably only if your commands are enum rather than real objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.