• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing request variables from Servlet to utility class

 
Mikey Kelly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A request reaches my servlet, with some variables set in it by the user via an html form.
I check what the "action" variable says, and forward to the appropriate JSP page. In one case, I want to do some processing with a utility class before forwarding to the JSP page.
I call the class like this -

NewAccountProcessor nap = new CSAccountProcessor();
boolean success = nap.processRequest(req);

However, when I try to read the form variables in req from the CSAccountProcessor() class, I get a null exception.
Is this expected behaviour? Do I need to read the req variables in the servlet and then explicitly pass them as parameters in my function call, instead of passing the whole request object? Or am I just doing something silly somewhere...

thanks.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post some of your code and the exception trace? At first glance, i would assume that you shouldn't have any problems passing the reqeust object as a parameter...
 
Makarand Parab
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikey Kelly
Well the request object should get passed to the utility clas where you can get the parameters, but good design would be to create a bean in the servlets, set the values in the bean and then pass the bean to the utility class. Passing request object to utility class scatters your code which can be tough for debugging. Handling all the request and response object at one place is always a better option.

Let me know what you think
More inputs are always welcomed

Regards
Makarand Parab
 
Javed Mohammed
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Makarand Parab. Having the utility calss to access request object also defies the MVC pattern. In case if we want to use a different client other than a servlet, then there is a problem in the utility calss.
 
Mikey Kelly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks,
I'll take a look at the form end of things. I must be going wrong there. Thanks for the bean suggestions too. I'll give that a try.
Mike
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I like to pass the Map from the request method getParameterMap()
this maps the parameter name to a String[] of values. This causes a little extra work in the utility side but there is a really good reason to do it.
The reason is you can now test your utility code OUTSIDE the servlet environment - so much easier, especially with complex utilities.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic