• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi Purpose Servlet

 
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that a servlet will do different things depending on what parameters are passed to it, can I put, for example, all the logic including error processing into a single servlet and simply call itself with different parameters to display different html to the user.

To elaborate, a logon servlet might accept a paramter which means display the username / password and submit button initially. Then when someone clicks submit the same servlet it called with a parameter that means, check the credentials and either display - "logon succeeded" or "logon failed".

I've not found an example of this yet, so it may be bad practice. If so please explain why.

Many thanks

Dave
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would not say that what you are saying is a bad idea , otherwise struts would not been having DespatchAction Class.

Here is what I think about servlets.

Servlets should be used for delegating calls to business logic sitting in different classes or forwarding to views / jsp's.

It would not be a bad idea to put logic specific to a particular functional module in one servlet.If some major changes to the module happens then a new servlet can be constructed and the previous version may be replaced.

But the class should not grow too large or else maintaining might be difficult in future.

Struts has a similar Action known as DispatchAction for this .
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These days I try to keep the servlet class small and pass off most of the real work to the "doTheNextThing()" method in another "worker" class. There the current user state and the passed parameters are used to decide what to do next.

Consider defining the worker class based on an interface so that the servlet only deals with interface methods - makes it easy to plug in an alternate worker based on whatever...

If you can design the worker class so that it can be tested outside the servlet environment, even better!

Bill
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do a search for "Front Controller" or "Servlet Command Pattern" you should find some example that do this.
As William said, it is better to break the different behaviors into different classes (commands or actions) and have the servlet do nothing more than delegate the task to these commands.
Doing so makes things a lot easier to test and maintain.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the time has come to make FrontMan available. I was going to wait until I had chance to write a JR Journal on its design, but that's probably not going to happen soon. Watch the Blatant Advertising forum late this evening or perhaps tomorrow...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Perhaps the time has come to make FrontMan available. I was going to wait until I had chance to write a JR Journal on its design, but that's probably not going to happen soon. Watch the Blatant Advertising forum late this evening or perhaps tomorrow...



Looking forward to it.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:


Looking forward to it.

https://coderanch.com/t/34646/ba/Front-Man
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic