Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a form that submits to a servlet that has optional parameters. If they are empty they are just ignored. This is the template I am using:

if (!(param = (String)params.get("httpParam")).equals("")) {
   // Handle parameter
}

It doesn't quite feel right. It works, but I think it could be more readable. Any suggestions?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I suggest extract code for getting parameter to separate method (see extract method refactoring's tehnique):

param = getOptionalParameter("httpParam");
if (! param.isEmpty()) {
    // Handle parameter
}
share|improve this answer

You can populate a map containing all your parameters and than convert that map into POJO:

    HashMap map = new HashMap();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
      String name = (String) names.nextElement();
      map.put(name, request.getParameterValues(name));
    }
    BeanUtils.populate(data, map);

Where 'data' is your POJO class with attributes corresponding to your parameter names.

share|improve this answer
1  
How about request.getParameterMap() instead of while loop? –  Alexander Vasiljev Dec 3 '11 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.