• 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

Servlet condition block moved to Helper class

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Servlet in Tomcat that works where it gets parameters from a form and I have condtions for the parameters:

Now I want to move this part of the Servlet into a helper class or bean and call it in the Servlet but not sure how to do this?
Here is what I have so far but need help. Please advise.

Helper.java


Then call it in Servlet like this?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like, you just want to play a bit with servlet in order to gain some understanding. If it is the case then go ahead try the thing you already have.
 
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can add two instance variables to your Helper class like fieldOne and fieldTwo with getters and setters.
From the Servlet you do the following:

1.Instantiate the Helper class
2.Call the Setter methods for fieldOne and fieldTwo
3.Call the conditionMethod() of the Helper class

Does this help?

Regards,
Srikkanth.M
 
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
There is an example app that uses the command pattern to accomplish what you want on my site.

In this example, I'm using a form parameter as the identifier for the commands. It is also common to parse the uri and use that as the identifier.

http://simple.souther.us
Look for SimpleCommand.


Rather than moving the if/else block to a helper class, the command pattern offers a way to eliminate the if/else block altogether.
 
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
I like to use the ServletRequest method getParameterMap() to extract all form parameters into a Map. Using the Map as input to a helper class, there is no longer a requirement for the helper to know anything about the servlet API.

This means you can test the helper outside the servlet environment. A really big help in fast development turnaround and debugging.

Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic