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;
 }
 }
asked Jun 6, 2013 at 14:08
\$\endgroup\$

1 Answer 1

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.

answered Jun 7, 2013 at 3:00
\$\endgroup\$
3
  • \$\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\$ Commented Jun 9, 2013 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\$ Commented Jun 10, 2013 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\$ Commented Jun 10, 2013 at 8:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.