• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi Purpose Servlet  RSS feed

 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • 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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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 .
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:


Looking forward to it.
http://www.coderanch.com/t/34646/ba/Front-Man
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!