I am a bit confused with this pattern Design Patterns web based applications. I am trying to develop simple login example. I have login.jsp with form and home.jsp for login success.
In ActionFactory class I have login in map and LoginAction which returns home. My ActionController class is as on link above and is mapped with /pages/*. What url should I use for calling loginAction? If I use /login front controller doesn't matches it and if I use /pages/login it matches it but when "home" comes to FrontController actions is null. Should I have action for home.jsp also? In that case how redirect is achieved?
ActionFactory
public class ActionFactory {
private static ActionFactory instance;
public ActionFactory() {
actions = new HashMap<>();
actions.put("login", new LoginAction());
}
public static ActionFactory getInstance() {
if (instance == null) {
return new ActionFactory();
} else {
return instance;
}
}
Map<String, Action> actions;
public Action getAction(HttpServletRequest request) {
return actions.get(request.getPathInfo().substring(1));
}
}
Servlet
@WebServlet(name = "ActionController", urlPatterns = {"/pages/*"})
public class ActionController extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Action action = ActionFactory.getInstance().getAction(request);
String view = action.execute(request, response);
if (view.equals(request.getPathInfo().substring(1))) {
request.getRequestDispatcher("/" + view + ".jsp").forward(request, response);
} else {
response.sendRedirect(view); // We'd like to fire redirect in case of a view change as result of the action (PRG pattern).
}
} catch (Exception e) {
throw new ServletException("Executing action failed.", e);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
LoginAction
public class LoginAction implements Action{
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
return "home";
}
}