• Post Reply Bookmark Topic Watch Topic
  • New Topic

FrontController

 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I have 2 questions.

1) How can I make sure every request goes through a front controller servlet?
So I have FrontController.java, ControllerA.java, ControllerB.java.
Process is as follows:
A) Front controller receives json string
B) Parses the json string to a java object using jackson.
C) Puts the parsed object in request scope and forwards it to one of the other controllers(ControllerA or ControllerB)

My question is? How can I make sure every single request that comes in goes through the FrontController.java? Can you please provide some code. Thank you.
So if they call ControllerA.java first, it should keep the header information and forward it to the FrontController.java

2) Is having a static method call in the doGet() of a servlet thread-safe? So, StringUtilities.isNullOrEmpty(...), is that thread-safe? What if you call that same method by multiple doGet requests simultaneously, since it's static.









 
E Armitage
Rancher
Posts: 989
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Bag wrote:Guys I have 2 questions.

1) How can I make sure every request goes through a front controller servlet?

You just make all your pages submit to the same url (that of the controller servlet). You would then need to pass an extra parameter to tell the controller where to forward the requests afterwards. Alternatively you could use a filter.
Greg Bag wrote:
2) Is having a static method call in the doGet() of a servlet thread-safe? So, StringUtilities.isNullOrEmpty(...), is that thread-safe? What if you call that same method by multiple doGet requests simultaneously, since it's static.

Yes it's safe. Whether the method is static or not does not make it thread safe or not thread safe. What is important is whether you are changing variables that are being accessed by other threads e.g if you declared instance variables in the servlet then methods that access those variables are not thread safe (whether they be static or non-static).
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Armitage you're always helping me! Thank you man.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See FrontMan in my signature below for an example of a front controller implementation.

You can also use a prefix mapping, such as as /whatever/* to map requests to the front controller, where * denotes the command to execute. This is a more modern design than using params.

Be careful about what you catch. You don't want to fields requests for images, scripts and the like.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!