Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet condition block moved to Helper class

 
Mike Jenkins
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?
 
Adeel Ansari
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.
 
Srikkanth Mohanasundaram
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
 
Ben Souther
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.
 
William Brogden
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!