title: Java servlet - implementing command pattern
body: <p>I'm trying to understand how to implement the 'Command design' into my java servlet. Here is an example of the servlet:</p>
  
  <pre><code>protected void processRequest(HttpServletRequest ...) {
  
      String action = request.getParameter("action"); 
      String destination = null;
  
      } if (action.equals("Home")) 
          destination = "Home";
      } else if (action.equals("Add")) {
           String var = request...
  
           try {
              db.add(var);
           } (DatabaseException e) {
              request.setAttribute("errorMessage", e.getMessage());
           }
           ...
      }
   }
  </code></pre>
  
  <p>What I understand is that every if statement should be converted to a separate class with an execute function that handles for example the adding of an object. What I don't understand is how the requests are handles, are they handles in the execute function and from there returned to the Servlet/JSP (my catched errors are put in a request and used in my JSP files)?</p>
  
  <p>I hope someone can give me a basic structure and show me how to begin implementing it. </p>