Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Understanding Front Controller Design Pattern

DZone's Guide to

Understanding Front Controller Design Pattern

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The front controller design pattern means that all requests that come for a resource in an application will be handled by a single handler and then dispatched to the appropriate handler for that type of request. The front controller may use other helpers to achieve the dispatching mechanism.

While reading the brief overview you may have thought about ActionServlet in Struts 1 or Dispatchers in Struts 2. Yes both of them are implementations of Front controller design pattern. But most of the developers that I have interacted to (specially the beginners) end their thought process of Front Controller at Struts only.

The Front Controller design pattern is applicable and useful for all kind of applications be it web or desktop applications and is not limited to any single programming language or framework. Before the rollout of MVC frameworks in the market, the design pattern was still in use.

The only downside was that not all application could make use of it because of the effort involved. Only the applications which had custom framework in place were using the design pattern.

So for example in case of a Java based web application, which is not using any MVC framework, all the requests would be mapped to a single servlet in web.xml file. This we know can be done by using the servlet mapping (same as we do for ActionServlet in case of Struts 1).

<web-app>    <servlet>        <servlet-name> FrontServlet</servlet-name>        <servlet-class> com.example.test.FrontServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>FrontServlet<servlet-name>        <url-pattern>/*</url-pattern>    <servlet-mapping></servlet-mapping></servlet-name></servlet-name></servlet-mapping></web-app>

Once the request reached the FrontServlet, it uses some configuration files to decide the appropriate dispatcher for the request and thus forwards the request using forward mechanism.

From http://extreme-java.blogspot.com/2011/04/understanding-front-controller-design.html

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
java ,patterns

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}