rflprp
Não sei se a forma que estou usando é a melhor, até agora está funcionando bem, e bem simples:
package br.com.dogato.controle;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.dogato.controle.actions.*;
public class PetController extends HttpServlet {
private HashMap actions = new HashMap();
public void init() throws ServletException {
actions.put("login", new ActionLogin());
actions.put("logout", new ActionLogout());
actions.put("erro", new ActionErro());
actions.put("cadUsu", new ActionCadastroCliente());
}
private Action getAction(String chave){
try{
System.out.println("Chave " + chave);
Action acao = (Action)actions.get(chave);
if(acao == null){
System.out.println("Acao nula");
return (Action)actions.get("login");
}
else{
System.out.println(((Action)actions.get(chave)).getClass());
return (Action)actions.get(chave);
}
}
catch(Exception e){
e.printStackTrace();
return (Action)actions.get("login");
}
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
service(arg0, arg1);
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
service(arg0, arg1);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String actionType = request.getParameter("actionType");
System.out.println(actionType);
Action acao = getAction(actionType);
acao.execute(request,response);
}
}