2

Take into a consideration a web application which has a few main features such as a Registration feature, Login feature, and once the user is Logged In, CRUD operation features. My Question is, do I have multiple servlets handling each specific task, or do I group it all into one main Controller servlet which is communicating to a Data Access Object.

I don't want to use any pre-exiting frameworks, and wanting to learn the proper way of implementing such functionality properly. If someone could give me a help in hand, it would be greatly appreciated.

Thanks.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Maff
  • 1,032
  • 4
  • 25
  • 42
  • Existing MVC frameworks have only one controller servlet. If you want to reinvent the wheel anyway, I'd suggest to go the same path. See also http://stackoverflow.com/q/3541077 Note that the controller servlet does usually not interact with the DAO at all. Instead, the request based actions and business services do. – BalusC Aug 23 '13 at 13:31

3 Answers3

2

Well, The number of controllers depends on the type of the feature you want to implement. If two or more features are similar (doing similar work) then you can easilly group these into one or two servlets.

But if the features you are implementing are different then having separate controller will be good.

Here are few reasons why having separate controller for separate and different features is better:

1.) Your application will be more modular

2.) Upgrade and maintance will be easier for these features, because for changing one feature you don't have to take other features down(which is not possible when implementing all in one controller servlet)

3.) Adding and removing new features will be easier, since you donn't have to touch other controllers.

4.) Debugging and error finding will be easier because every controller will have specific task so the components for debugging will reduce effectively.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • Thank you, excellent answer. Although, in your case what would you group together? (Login, Register), (CRUD operations)? And if the case were to be having several different controllers, is it possible to maintain a sesison from the login point entry?(One servlet to another) – Maff Aug 23 '13 at 11:41
  • If I had to group features then I will put `logincheck` and `redirection to login page` in one group.`Login` and `Registration` are very different things, because with login you only have to deal with a `db` to verify the username and password(No updation to DB), but with registration you have to create new entries in `DB` and you may have to update several other Databases too. So I will keep `login` and `registration` separate. You can put `authorizations` for different resources in one group. – me_digvijay Aug 23 '13 at 11:53
  • Also `selecting different views` for different purposes based of some condition can be grouped together. ALSO, FOR YOU SECOND QUESTION>> AS SOON AS A USER HITS YOU APPLICATION IF NO SESSION EXISTS A NEW ONE CREATED NOT MATTER WHICH PAGE IT IS ACCESSING. SO YOU CAN MANNAGE SESSIONS EVEN BEFORE THE LOGIN POINT. – me_digvijay Aug 23 '13 at 11:57
  • See this is exactly what I need. Thank you very much for your responses and great answers. Do you have any known tutorials which are good, and I could learn these important aspects from? In text books, they don't quiet give you walk through's of an actual complete working web application. If you have any good resources, please forward them if you can. Thanks – Maff Aug 23 '13 at 12:00
  • If you are into web application development, I would suggest buying the `Head First Servlets and JSP`. It is one of the best source where you learn about these things veery easily. http://shop.oreilly.com/product/9780596005405.do – me_digvijay Aug 23 '13 at 12:04
  • Yeah, well I am currently only a student, although I really want to focus on web application developtment especially. I am pretty new to this, so I am trying to grasp my understanding around particular concepts. I have plenty of questions, although I don't want to bombard you with them. – Maff Aug 23 '13 at 12:06
  • Stackoverflow is the best place to bombard your questions(but before that you need to think a bit and search for solution). Happy Learning :) – me_digvijay Aug 23 '13 at 12:11
  • Exaclty, its all about seeking for a solution. well I was with this question, although it was concrete. And this was my only option. Thanks :) – Maff Aug 23 '13 at 12:14
1

Having a single entry point for your web application can ease your life to an extent. And that is why I am in favor of writing a single controller to accept all the incoming requests. It will be easier to debug the code when you have single point of entry than multiple. The controller should be simple and efficient as its job should be to dispatch the request to proper handler(other class).

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Thank you immensely for replying. I understand your perspective, although do you find that the servlet class gets really jammed up with code? – Maff Aug 23 '13 at 11:34
  • @user2660806 When I am saying controller, i mean a single servlet accepting all the requests. Servlet never hampers performance until we don't put some junk code into it. – Juned Ahsan Aug 23 '13 at 11:42
0

My suggestion on your question if you have multiple operation then you can use multiple controller for example if their is task related on user then you can only create one controller for user related task and write methods/ request mapping in that user controller. Like this way you can create for other operations in separate controller this will help you in simplicity of your project.

pravin
  • 151
  • 2
  • 3
  • 12