4

Just wondering if there is a more elegant or standard way to handle the optional parameters or if you have to check if every one is null. I have 10+ optional parameters so it is getting somewhat ugly.

Ideally I would like something like the bash command: getopts.

public class MapImageServlet extends HttpServlet {
    ... constructor and other methods ...
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // OPTIONAL PARAMETERS
        if(request.getParameter("boarderSize") != null){
            double boarderSize = Double.valueOf(request.getParameter("boarderSize");
        }

        if(request.getParameter("boarderThickness") != null){
            double boarderThickness = Double.valueOf(request.getParameter("boarderThickness");
        }

        if(request.getParameter("boarderColor") != null){
            double boarderColor = Double.valueOf(request.getParameter("boarderColor");
        }
        ... do stuff with the parameters ...
    }
    ... other methods ...
}
2
  • Don't declare the variable inside your if statement, it won't be available to other scopes. Commented Jun 13, 2011 at 20:14
  • @Marcelo, sorry about that. Normally would not do that, haha. Commented Jun 13, 2011 at 20:25

4 Answers 4

2

Write a utility like this

public class MapImageServlet extends HttpServlet {
//... constructor and other methods ...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // OPTIONAL PARAMETERS
     boarderSize = ParamUtil.getDoubleValue(request,"boarderSize", defaultValue);

     boarderThickness = ParamUtil.getDoubleValue(request, "boarderThickness", defaultValue);

     boarderColor = ParamUtil.getDoubleValue(request,"boarderColor" , defaultValue);
     //... do stuff with the parameters ...
}

}
public class ParamUtil
{
public static double getDoubleValue(ServletRequest request, String paramName, double defaultValue)
{
     if(request.getParameter(paramName) != null){
        return Double.valueOf(request.getParameter(paramName));
    } else{
        return defaultValue;
    }
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

I thought that there would already be something made like this already in the servlet but I guess that it is easy to make my own.
2

Aren't you looking for: ServletRequest#getParameterMap?

1 Comment

Incase you are stuck on an old WAS or something, getParameterMap is in JEE 5.0 as well.
0

Generally, I have used Apache beanutils to pull information from request parameter map. BeanUtils provides a nice interface that hides all this information from you ...

MyJavaBean mjb = new MyJavaBean();
BeanUtils.copyProperties(mjb, request.getParameterMap());

.... 
// do stuff with mjb properties
logger.debug(mjb.getBorderThickness());
logger.debug(mjb.getBorderSize());
// etc

A little extra work setting up the javabean but easy to use going forward.

Comments

0

If you are developing this from scratch I suggest you go with a framework like Spring MVC or Struts. These frameworks capture the input and provide you with a ready to use bean with all the form data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.