Design Pattern

Front Controller Design Pattern – Core J2EE Patterns

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

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago