One of the patterns that is commonly used in web application architecture design is FrontController.In this tutorial we will show you how to implement FrontController not by using any framework but developing it from scratch ony with Servlets JSPs and of course command classes (Controller classes in this tutorial).

In general FrontController pattern states that the entry point of all incoming requests is a class which is responsible for processing and delegating request to the appropriate controller object which is in turn rensponsible for gathering the necessary data and define the appropriate jsp for rendering  the data.

We begin by defining the Controller interface that all Controller classes will implement.

package com.javaonly.controllers;

import javax.servlet.http.HttpServletRequest;

public interface Controller {
    
    public void execute();
     public void init(HttpServletRequest request);
     public String getReturnPage();
}

The next step is to define the abstract class AbstractController that controllers will extend

package com.javaonly.controllers;

import com.javaonly.controllers.Controller;
import javax.servlet.http.HttpServletRequest;

public abstract class AbstractController implements Controller {

    private HttpServletRequest request;
    protected String returnPage;

    public void init(HttpServletRequest request) {
        this.setRequest(request);
    }

    public void setReturnPage(String page) {
        returnPage = page;
    }

    public String getReturnPage() {
        return returnPage;
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
}

After the definition of the AbstractController class we proceed with defining the ControllerFactory class that will be used by FrontController servlet to instantiate the appropriate Controller class according to the incoming request.

package com.javaonly.controllers;


import javax.servlet.http.HttpServletRequest;

public class ControllerFactory {

    public static final Controller getControllerByClass(Class actionClass) {
        try {
            Controller controller = (Controller) actionClass.newInstance();

            return (Controller) actionClass.newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
            
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            
        } catch (ClassCastException e) {
            e.printStackTrace();
            
        }
        return null;
    }

    public static final Controller getControllerByFullClassName(String className) {
        try {
            String name = "com.javaonly.controllers." + className + "Controller";
            Class actionClass = Class.forName(name);
            return getControllerByClass(actionClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Now that the ControllerFactory class is ready lets proceed by defining the controller for the home page controller which in our case will be IndexController

package com.javaonly.controllers;

import com.javaonly.Daos.FooDao;
import com.javaonly.Daos.DaoFactory;
import com.javaonly.Daos.BarDao;
import com.javaonly.Daos.FooBarDao;
import com.javaonly.hibernate.HibernateUtil;
import java.util.List;

/**
 *
 * @author master
 */
public class IndexController extends AbstractController{

    public void execute() {
        FooBarDao fooBarsDao=DaoFactory.getFooBarDao();
      BarDao barsDao=DaoFactory.getBarDao();
       FooDao foosDao=DaoFactory.getFooDao();
       HibernateUtil.beginViewTransaction();
        List fooBars = fooBarsDao.getAll();
        List foos=foosDao.getAll();
        List bars=barsDao.getAll();

        HibernateUtil.commitTransaction();
       
       this.setReturnPage("/index.jsp");
       this.getRequest().setAttribute("foos", foos);
       this.getRequest().setAttribute("bars",bars);
       this.getRequest().setAttribute("fooBars", fooBars);
       
    }
    
}

The final step is to create the FrontController servlet.In our case the incoming request will be www.java-only.com/Index.javaonly:

package com.javaonly.servlets;

import com.javaonly.controllers.Controller;
import com.javaonly.controllers.ControllerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FrontController extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();


    }

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the request's url
            String url = request.getRequestURL().substring(request.getRequestURL().lastIndexOf("/") + 1, request.getRequestURL().length());
            //get Controllers name from request
            String controller = url.substring(0, url.indexOf("."));
            //Instantiate controller class
            Controller control = ControllerFactory.getControllerByFullClassName(controller);
            //initialize controller
            control.init(request);
            
            control.execute();
            RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(control.getReturnPage());
            requestDispatcher.forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
}