1

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";
    }
}
Community
  • 1
  • 1

1 Answers1

0

I guess this post could help you.

How come request.getPathInfo() in service method returns null?

You'd like to use HttpServletRequest#getServletPath() and the url is /pages/login

Easy fix: call the page "login.jsp" ,the execute method of the Action should return the string "login" and the url to test is again "/pages/login"

Community
  • 1
  • 1
Antonio
  • 644
  • 5
  • 17
  • My problem is not at that line of code. If url /pages/home comes to ActionController I've got action object null because home doesn't exist in actions map. My question is should I put home action in actions map and if I should how can I redirect user to home.jsp? – Igor Dragic May 08 '15 at 11:30
  • the url goes to the ActionController only if the path has /pages urlPatterns = {"/pages/*"}) in the annotation, while you should to /home.jsp when redirect or forward – Antonio May 08 '15 at 11:33
  • So you are saying that home.jsp shouldn't go through ActionController? – Igor Dragic May 08 '15 at 11:39
  • I am saying that when redirect you shouldn't append the /pages/ prefix otherwise goes again in the action controller. – Antonio May 08 '15 at 11:40