Take the 2-minute tour ×

I'm thinking of implementing Front Controller in my J2EE application. Could you please suggest the same with few links (with source code examples) & any standards to follow?

Best regards

share|improve this question
How about the Spring MVC framework? springsource.org –  darren Jan 13 '10 at 20:50
J2EE is replaced by Java EE since more than 3 years. Keep yourself updated. –  BalusC Jan 13 '10 at 22:41
Yeah, I know but I generally go with that ... anyways thanks. –  user237865 Jan 14 '10 at 9:35

3 Answers 3

up vote 10 down vote accepted

To start, create a Servlet which listens on a certain url-pattern, e.g. /pages/*. Implement the service() method to lookup the action associated with the request method (GET, POST, etc) and pathinfo (the URL part after the servlet's url-pattern).

Basic example:

protected void service(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
  View view = new View(request, response);
  Action action = ActionFactory.getAction(request);

The Action interface should represent an unit of work. You can implement it to do the necessary business logic:

public interface Action {
  void execute(View view);

The ActionFactory should maintain the classes implementing Action in sort of Map<String, Action> where the String key represents less or more a combination of the request method and pathinfo. You could then get an Action as follows:

public static Action getAction(HttpServletRequest request) {
  return actions.get(request.getMethod() + request.getPathInfo());

The View should represent the request scoped context which the Action can work with. In the navigate() you could forward the request to a JSP for display:

public void navigate() {
  String path = "/WEB-INF" + request.getPathInfo() + ".jsp";
  request.getRequestDispatcher(path).forward(request, response);

That should get you started (note that I left all obvious checks such as null pointers away to make the examples less cluttered, that's up to you now).

There is however more to take account with in the whole story, such as validation, conversion, event handling, input value mappings, localization, dependency injection, etcetera. That's all with all quite a work. The more decent MVC frameworks takes most of this all into account, such as Sun JSF, Apache Struts, Spring MVC, Stripes, etcetera. If you have never done any of them, then I strongly recommend to do so before homegrowing one, otherwise you would end up with a waste of time.

share|improve this answer
When I read these documentation about Spring MVC, it always talks about Servlet engine to be Tomcat. Does it make any different if my servlet engine is Glassfish? Does glassfish read the content of web.xml? –  Thang Pham Apr 18 '10 at 18:53
@Harry: Spring is a bit anti-Sun. –  BalusC Apr 18 '10 at 19:10
ohhh god, sad news –  Thang Pham Apr 18 '10 at 19:54
@Harry: I didn't mean to say that Spring won't work on GF. It will work as good. –  BalusC Apr 18 '10 at 20:11
@BalusC would be nice if you extend your answer with an example of action.execute(), where you would explain how the controller passes data/helper to the view via request.setAttribute(). I think that it's an important concept to show.. –  yegor256 Feb 20 '11 at 14:39

The web is full of mature java web frameworks. I can't see a reason to create a new one with two exceptions: 1. You have some very non-standard environment/flow and need something very, very specific to your needs. 2. You simply want to go through the exercise of creating a framework to learn about the ins and outs of httpservlet development.

JSR 154 defines the servlet specs. Find it here: http://jcp.org/en/jsr/detail?id=154

Spring is one of the current popular frameworks and the one that works for me. Struts was one of the first very popular frameworks that has started to decline in popularity. Find a good list of frameworks along with discussion at http://java-source.net/open-source/web-frameworks

share|improve this answer

How about the Spring MVC framework?

share|improve this answer
No I cannot use Spring, looking for a custom build. any suggestions on that, thinking of implementing my own Front Controller!! –  user237865 Jan 13 '10 at 20:56
I meant look at the sourcecode used by Spring. They implement the Front Controller pattern as part of the MVC package. –  darren Jan 13 '10 at 21:30
Spring is too big to explore :) –  user237865 Jan 14 '10 at 9:16
GO WITH SPRING! –  Spider Jun 6 '11 at 16:40

Your Answer


