2
\$\begingroup\$

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 < MIN_NUM_PARAMETERS)
    {
        out.println( JSONHelper.writeStatus(false, Status.TOO_FEW_ARGS.getMessage()));
        return;
    }

    // validate parameters
    if (parameters.length > MIN_NUM_PARAMETERS)
    {
        try
        {
        maximumResults = Integer.parseInt(parameters[2]);
        } catch (NumberFormatException nfe)
        {
        out.println( JSONHelper.writeStatus(false, Status.INVALID_MAX_RESULTS.getMessage()));
        return;
        }
    }
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ It's a viable solution but I'd like to quote: Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. I think it's a little too much to check parameters. \$\endgroup\$ – W.K.S Jun 9 '13 at 14:00
  • \$\begingroup\$ That's right, but I think this regexp is simple enough for you to test, it is not a real problem here. Is your second parameter required too? If not, you can change the regexp to http://www.hostname.com/servletname/([^/]+)/(\\d+)$, you have one check now. \$\endgroup\$ – simpletron Jun 10 '13 at 3:02
  • \$\begingroup\$ Your code worked perfect. I used it with another page and got the regex wrong which was a bit of a hassle. In the end, I'd prefer something that's easy to read though I do think your answer is a valid way of cleaning up the code. \$\endgroup\$ – W.K.S Jun 10 '13 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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