This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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...
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...
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
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
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.
Joined: Aug 09, 2005
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
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