How to refactor large if else block in java servlet front controller

Ashley Talbert

I am currently using a front controller pattern for my servlet and utilize a large if-else if structure in the doGet() and doPost() methods; however, my application is starting to add more and more functionality and as result the if-else blocks are getting out of control. I was wondering what are some popular strategies for handling this type of situation (while maintaining a front controller).

All I could think to do was maybe use a hashtable that maps paths (from the request url) to helper methods that determines the appropriate JSP (and sets attributes).

I liked the look of the Spring framework; however, I am currently not able to port to a framework, how does something like Spring avoid the problem I am facing?

Rafa

Independently of the framework or technology, I've faced several times your case and what I always do is to use a command pattern:

https://en.wikipedia.org/wiki/Command_pattern

and in the command handler I use a Factory pattern that instantiates the appropriate class to handle the request with the information of the command object.

With this architecture you are veeery flexible and it is quite easy to implement :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java: How to refactor my code to reduce redundancy?

From Dev

Refactor if-else-if structure

From Dev

Design program to avoid a large if-else block

From Dev

How do I send large (over 64k) http responses from a java servlet in a jetty server?

From Dev

How to apply if and else angularjs controller

From Dev

Avoiding a large amount of if-else statements in java

From Dev

Refactor If/Else Statements

From Dev

How to refactor/rename tables, fields and values in a large database quickly?

From Dev

How to refactor this java code

From Dev

How to refactor this HashSet in Java

From Dev

How to refactor a repetitive line of code java

From Dev

How to correctly refactor this big and with very similar code if/else statements?

From Dev

Magento - How to extend Front Controller

From Dev

should refactor this if else

From Dev

Java casting in if/else block

From Dev

Unnecessary unboxing in java - how to refactor the code?

From Dev

How to refactor these if else statements to a function

From Dev

What's the best way to refactor a large switch statement in java?

From Dev

How to separately checksum each "block" of a large file

From Dev

How can I refactor my if-else statement?

From Dev

how to refactor this multi-nested if else block

From Dev

How to refactor a synchronization block based on string

From Dev

How to block and unblock a servlet thread?

From Dev

How to apply a deferred object to a large block of code?

From Dev

Refactor a case structure in a controller

From Dev

PHP refactor if else

From Dev

How servlet controller know which method to use?

From Dev

Refactor large controller method into model

From Dev

how to write if else in the html td . if block and else block both are executing

Related Related

  1. 1

    Java: How to refactor my code to reduce redundancy?

  2. 2

    Refactor if-else-if structure

  3. 3

    Design program to avoid a large if-else block

  4. 4

    How do I send large (over 64k) http responses from a java servlet in a jetty server?

  5. 5

    How to apply if and else angularjs controller

  6. 6

    Avoiding a large amount of if-else statements in java

  7. 7

    Refactor If/Else Statements

  8. 8

    How to refactor/rename tables, fields and values in a large database quickly?

  9. 9

    How to refactor this java code

  10. 10

    How to refactor this HashSet in Java

  11. 11

    How to refactor a repetitive line of code java

  12. 12

    How to correctly refactor this big and with very similar code if/else statements?

  13. 13

    Magento - How to extend Front Controller

  14. 14

    should refactor this if else

  15. 15

    Java casting in if/else block

  16. 16

    Unnecessary unboxing in java - how to refactor the code?

  17. 17

    How to refactor these if else statements to a function

  18. 18

    What's the best way to refactor a large switch statement in java?

  19. 19

    How to separately checksum each "block" of a large file

  20. 20

    How can I refactor my if-else statement?

  21. 21

    how to refactor this multi-nested if else block

  22. 22

    How to refactor a synchronization block based on string

  23. 23

    How to block and unblock a servlet thread?

  24. 24

    How to apply a deferred object to a large block of code?

  25. 25

    Refactor a case structure in a controller

  26. 26

    PHP refactor if else

  27. 27

    How servlet controller know which method to use?

  28. 28

    Refactor large controller method into model

  29. 29

    how to write if else in the html td . if block and else block both are executing

HotTag

Archive