Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet condition block moved to Helper class  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!