This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

One servlet or many?  RSS feed

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys/gals,

just wondering is it better to have one servlet that covers an entire web application or a servlet 'per action'. Im learning how servlets work now and have noticed that they have one method each for get and post requests. I also see that you can use the servlets urlPatterns parameter to specify what urls should invoke it. So should I have a servlet for each action or should I for example, split the doGet/doPost methods something like so:


I don't know if that makes sense or not?

Thanks,
Alan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you've posted is a well-known anti-pattern. if you've got a class doing a lot of if-then-else's it's usually not a good sign. And certainly not OO.

There are many way to handle the actions in a web app. If you're not using a framework -- which usually handles mapping URL's to classes or even individual methods -- the usually approaches are to have a servlet per action (which makes for a messy deployment descriptor) or to use a Front Controller.

See this article for more information on all this.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:which makes for a messy deployment descriptor.


I agree the if-else could get very messy. About the deployment descriptor though, is it even necessary to use one (with servlet mappings) now that annotations are used? I'm no expert but I'm starting to learn Java EE and want to start in the right place!

Thanks for the quick response,
Alan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Servlets3, the deployment descriptor messiness is vastly minimized by the use of annotations.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If you are using Servlets3, the deployment descriptor messiness is vastly minimized by the use of annotations.


Ok, thanks. Could I ask one more question... When would I ever have to use the response object? All I have used so far is the request objects to retrieve form data, which can also pass that data along by setting them to request attributes. Its a bit confusing. I would have though that request objects retrieve data and response objects are used to forward the data.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's usually best to ask new questions in new topics.

The response object is used to create the HTTP response to the request. Forwarding values from the controller to the JSP is not part of the response, which will be automatically generated by the JSP.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!