<p>The Front Controller Design Pattern is one of the J2EE software design patterns. Several pattern catalogs have it listed in them. It is related to and used in the design of web applications. The front controller is responsible for handling all the requests for a website. For web application developers, it can be a very useful structure, as it allows the developers the flexibility and the ability to reuse the code without having to add again and again.</p>
<h2><strong>Front Controller Design Pattern</strong></h2>
<p>In web applications, the front controllers are used to implement the workflows. It is not necessarily required in the process, but it helps in controlling when the user navigates through a number of related pages.</p>
<p>Use a Front Controller as the initial point of contact for handling all related requests. The Front Controller centralizes control logic that might otherwise be duplicated, and manages the key request handling activities.</p>
<div style="text-align: left;">
<blockquote>
<p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/intercepting-filter-design-pattern/" href="https://dineshonjava.com/intercepting-filter-design-pattern/">Intercepting Filter Pattern</a></li>
<li><a title="https://dineshonjava.com/context-object-design-pattern/" href="https://dineshonjava.com/context-object-design-pattern/">Context Object Pattern</a></li>
<li><a title="https://dineshonjava.com/application-controller-design-pattern/" href="https://dineshonjava.com/application-controller-design-pattern/">Application Controller Pattern</a></li>
<li><a title="https://dineshonjava.com/view-helper-design-pattern/" href="https://dineshonjava.com/view-helper-design-pattern/">View Helper Pattern</a></li>
<li><a title="https://dineshonjava.com/composite-view-design-pattern/" href="https://dineshonjava.com/composite-view-design-pattern/">Composite View Pattern</a></li>
<li><a title="https://dineshonjava.com/dispatcher-view-design-pattern/" href="https://dineshonjava.com/dispatcher-view-design-pattern/">Dispatcher View Pattern</a></li>
<li><a title="https://dineshonjava.com/service-to-worker/" href="https://dineshonjava.com/service-to-worker/">Service to Worker Pattern</a></li>
</ul>
</blockquote>
</div>
<p><strong>For example,</strong> navigating through a number of pages that are used in an online shopping website while purchasing. It is difficult to control the navigation when every page is individually responsible for navigation. The front controller can be implemented as an object in Java. It can also be implemented in a scripting language as a script, for example, Python, Ruby or PHP. For every single request of a web session, this script is called.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<p><strong>For example</strong>, index.php is a script, which will be responsible for handling all the tasks that are common to the framework or an application. These tasks might include, caching, input filtering and handling. The front controller is able to instantiate further objects or to handle any particular tasks, it would call methods- but these actions depend on the specific requests.</p>
<p>In an alternative to the front controller, there are certain individual scripts, such as order.php or login.php. These can be used to satisfy the specific requests. Every single script would be required to duplicate the objects or codes which are common to all tasks. But, every single script might have the flexibility to perform the specific tasks required.</p>
<h2><strong>Structure of the Front Controller Design Pattern</strong></h2>
<p>The structure of front controller design pattern is such that there is a controller, dispatcher, helper, and view. All of these objects have different tasks to perform and different responsibilities to cater.</p>
<p><img class="aligncenter wp-image-3598 size-full" src="https://dineshonjava.com/wp-content/uploads/2017/12/FrontController.gif" alt="Front Controller Design Pattern" width="621" height="220" /></p>
<h3>Front Controller</h3>
<p>The controller is more like a gateway for the users in order to handle the requests within the system. It can play multiple roles like for instance, it can be a delegating helper or it can initiate contact retrieval.</p>
<h3>Dispatcher</h3>
<p>The dispatchers cater management of the view output and the navigation. A helper is responsible for helping the user to view or control the process.</p>
<h3>View</h3>
<p>The view is responsible for displaying information to the client.</p>
<h2><strong>Example of the Front Controller Pattern</strong></h2>
<p>There are following classes based the Front Controller pattern.</p>
<ul>
<li><strong>DispatcherServlet</strong> in the Spring Framework</li>
<li><strong>FilterDispatcher</strong> in the Struts 2 Framework</li>
</ul>
<h2><strong>Sample Implementation for Front Controller</strong></h2>
<p>Let&#8217;s see the following sample example for this pattern&#8217;s implementation.</p>
<h3>Step 1 : Creating number of views.<br />
CompanyView.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.frontcontroller; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class CompanyView { 
	 
	public void view(){ 
		System.out.println("Rendering Company Home Page!!!"); 
	} 
} 
 
</pre>
<h3>EmployeeView.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.frontcontroller; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class EmployeeView { 
	 
	public void view(){ 
		System.out.println("Rendering Employee Detail Page!!!"); 
	} 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create Dispatcher class.<br />
Dispatcher.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.frontcontroller; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class Dispatcher { 
	 
	private EmployeeView employeeView; 
	private CompanyView companyView; 
	 
	public Dispatcher(){ 
		employeeView = new EmployeeView(); 
		companyView = new CompanyView(); 
	} 
 
	public void dispatch(String request){ 
		if(request.equalsIgnoreCase("EMPLOYEE")){ 
			employeeView.view(); 
		}else{ 
			companyView.view(); 
		}	 
	} 
} 
 
</pre>
<h3>Step 3: After creating Dispatcher class, lets create front controller class whose have dispatcher object.<br />
FrontController.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.frontcontroller; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class FrontController { 
	 
	private Dispatcher dispatcher; 
 
	public FrontController(){ 
		dispatcher = new Dispatcher(); 
	} 
 
	public void dispatchRequest(String request){ 
		System.out.println("Page requested: " + request); 
		dispatcher.dispatch(request); 
	} 
} 
 
 
</pre>
<h3>Step 4: Let&#8217;s create a demo class to use the FrontController to demonstrate Front Controller Design Pattern.<br />
FrontControllerPatternDemo.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.frontcontroller; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class FrontControllerPatternDemo { 
 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) { 
		FrontController frontController = new FrontController(); 
		frontController.dispatchRequest("COMPANY"); 
		frontController.dispatchRequest("EMPLOYEE"); 
	} 
 
} 
 
</pre>
<h3>Step 5: Let&#8217;s run the above demo class and verify the output.</h3>
<pre class="highlight">Page requested: COMPANY 
Rendering Company Home Page!!! 
Page requested: EMPLOYEE 
Rendering Employee Detail Page!!! 
 
</pre>
<p>Happy J2EE design Pattern learning.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/context-object-design-pattern/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/application-controller-design-pattern/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3597'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…