How can I tidy up this servlet parameter check code? One of the servlet in my web app takes parameters in the form www.service.com/servletname/parameter1/parameter2.

The first parameter is required and the second parameter is an integer. 
I've made some code to validate the parameters but it looks really really messy. Is there a nicer and cleaner way I can write the following:

Thanks :)

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType(CONTENT_TYPE);
    PrintWriter out = resp.getWriter();
    int maximumResults = defaultMaxResults;

    // check that parameters were provided
    if (req.getPathInfo() == null)
    {
       out.println( JSONHelper.writeStatus(false, Status.TOO_FEW_ARGS.getMessage()));
        return;
    }

    // seperate parameters
    String[] parameters = req.getPathInfo().split(PARAMETER_SEPERATOR);

    if (parameters.length &lt; MIN_NUM_PARAMETERS)
    {
        out.println( JSONHelper.writeStatus(false, Status.TOO_FEW_ARGS.getMessage()));
        return;
    }

    // validate parameters
    if (parameters.length &gt; MIN_NUM_PARAMETERS)
    {
        try
        {
        maximumResults = Integer.parseInt(parameters[2]);
        } catch (NumberFormatException nfe)
        {
        out.println( JSONHelper.writeStatus(false, Status.INVALID_MAX_RESULTS.getMessage()));
        return;
        }
    }
In my opinion you could use the Regex to solve this

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType(CONTENT_TYPE);
    PrintWriter out = resp.getWriter();
    int maximumResults = defaultMaxResults;

    // you could create pathPattern as a Class field
    Pattern pathPattern = Pattern.compile("http://www.hostname.com/servletname/([^/]+)(/(\\d+))?$");

    Matcher m = pathPattern.matcher(req.getPathInfo());
    if(m.find()) {
         String param1 = m.group(1);
         String param2 = m.group(2); // NOTE param2 may be null
         // TODO your business here
         return;
    } else {
         // TODO throw your exception here
    }

}


You can change the pathPattern to meet your need. Hope this help. Feel free to response.